Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Bytes[] to construct an empty Bytes #11897

Merged
merged 1 commit into from
Mar 17, 2022

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Mar 14, 2022

Bytes[] in normal code fails to compile:

There was a problem expanding macro '[]'

Code in test.cr:1:1

 1 | Bytes[]
     ^
Called macro defined in src/slice.cr:33:3

 33 | macro [](*args, read_only = false)

Which expanded to:

 > 2 |     # asking if @type was instantiated or if T is defined
 > 3 |     
 > 4 |       UInt8.slice(, read_only: false)
                        ^
Error: unterminated call

But Bytes fully specifies the type of the constructed object, so allowing Bytes[] should not be problematic. This is similar to #10192 and also related to #11879 because inspecting an empty Bytes produces Bytes[].

Slice[] is still disallowed because the element type could not be inferred (it is technically NoReturn, which is not very useful either).

@j8r
Copy link
Contributor

j8r commented Mar 14, 2022

Will it be consistent with the language? Slice(UInt8)[] doesn't compile either, and the type is defined.
BitArray[] also fails to compile, but BitArray.new 0 does.

Of course all of this could be fixed in other PRs.

@HertzDevil
Copy link
Contributor Author

.[] is not part of the language, only the standard library, unlike {}; it might not even be variadic at all, e.g. ENV.[]. So there is nothing here to be consistent with.

For that matter, Number.[] also works with zero arguments, and StaticArray.[] does not make sense, similar to Slice. BitArray is mentioned here.

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think this is fine but I feel Bytes.empty is clearer 🤷

@HertzDevil
Copy link
Contributor Author

I'd say Bytes[] is clearer when juxtaposed with other Bytes[...] literals and also within #inspect. Following that discussion Bytes[] should be a well-formed constructor too.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: ArrayLiteral#splat 🤟

@straight-shoota straight-shoota added this to the 1.4.0 milestone Mar 14, 2022
@straight-shoota
Copy link
Member

Slice(UInt8)[] doesn't compile either, and the type is defined.

That's actually a limitation of the language. Actually, not for this particular call signature - it would be trivial to implement: def Slice.[](); new(0); end. But it wouldn't be able to work the same way when there are arguments (like Slice(UInt8)[1, 2, 3]). For that we have the macro implementation of Slice.[], but this doesn't work on a generic instance type.

@HertzDevil
Copy link
Contributor Author

HertzDevil commented Mar 14, 2022

It appears there are quite a lot of uses for Bytes.empty. I don't know why .empty is defined only for Slice but not for other containers like Array and Hash. Is it because those containers' array/hash-like literals require an argless .new which conveniently doubles as .empty?

@asterite
Copy link
Member

It seems I added that method. The reason was:

  • Array and Hash can grow, so they can start empty and then add elements. Slice can't grow
  • Slice(T).new without arguments reads a bit strange
  • I made sure it uses Pointer.null instead of something else

But now with Slice.[] it's probably redundant.

@straight-shoota
Copy link
Member

But now with Slice.[] it's probably redundant.

Slice.[] doesn't actually work, though. Only Bytes[] does. See my previous comment.

@straight-shoota straight-shoota merged commit 39efd0c into crystal-lang:master Mar 17, 2022
@HertzDevil HertzDevil deleted the bug/empty-bytes branch March 17, 2022 23:54
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants