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

fix unexpected behavior in Slice(T)[] with T is numeric type #3723

Closed
wants to merge 1 commit into from

Conversation

firejox
Copy link
Contributor

@firejox firejox commented Dec 17, 2016

There is an unexpected behavior in current Slice(T)[...]. It will cast argument with non-numeric type
to T when T is numeric type as the below case.

pp Bytes['1', 1] # Bytes['1', 1] # => Bytes[1, 1]

@asterite
Copy link
Member

This is because Bytes[...] will use UInt8.new on the arguments. This ends up invoking to_u8 on the arguments. For String and Char this ends up giving the decimal value of the content. I don't see a big problem with that, usually Bytes[...] is used in specs, not so much in real code (because it allocates memory each time)

@firejox
Copy link
Contributor Author

firejox commented Dec 18, 2016

@asterite So is this fine to make non-numeric type with to_u8 method use Bytes[...] ?
I think do some type checking would be better.

@ysbaddaden
Copy link
Contributor

@firejox yes, because Bytes[1, 2] would fail otherwise, since 1 and 2 are Int32, not UInt8...

@firejox firejox closed this Dec 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants