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

Use Slice.empty and Bytes consistently throughout the codebase #3681

Merged
merged 2 commits into from Dec 18, 2016

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Dec 12, 2016

Title says it all :)

@RX14
Copy link
Contributor

RX14 commented Dec 12, 2016

Is there really any reason to use Bytes instead of Slice(UInt8)?

@Sija
Copy link
Contributor Author

Sija commented Dec 12, 2016

@RX14 I guess it's the same reason that brought alias Bytes into being. I see it twofold:

  • Descriptiveness: Bytes communicates clearer message than Slice(UInt8)
  • Speed: Bytes is 7 characters less than Slice(UInt8)

@bcardiff
Copy link
Member

We should resolve #3551 to avoid changing this again I think. Just in case Bytes turns to Binary as @kostya suggested.

@Sija
Copy link
Contributor Author

Sija commented Dec 12, 2016

@bcardiff IMO it'll be easier to change it afterwards just by find-and-replace Bytes to Binary. Right now there are both forms present throughout the codebase.

@asterite
Copy link
Member

What happens if we eventually decide to add a BinaryTree type to the standard library?

@ysbaddaden
Copy link
Contributor

The Bytes = Slice(UInt8) alias was introduced as a convenience. Since there are some doubts on the chosen name, we should stick to the actual type, that is Slice(UInt8), instead of the alias for the time being.

If we currently use a mix, maybe we should change the occurrences of the Bytes type to the original type, instead.

@asterite
Copy link
Member

I actually like Bytes: it's short and clear, and I honestly don't see it going away just because we have String#bytes. My comment about BinaryTree was about saying that if we eventually chose the name Binary instead of Bytes, it will be odd to say "Oh, but now we have BinaryTree and it's not a tree of Binary, isn't this an inconsistency? And String#bytes isn't very useful, just two occurrences in the standard library, vs. more than 100 of Bytes.

@asterite
Copy link
Member

Like in Esperanto there's the suffix "-eg-" which means "big", so you can say "domo" = "house" and "domego" = "mansion". But then you have "kolo" = "neck" and "kolego" = "big neck", but "kolego" is also "collegue". But they didn't start renaming suffixes or words when they found this out, it's honestly not a big deal and I was able to speak Esperanto despite of this small inconsistency 😸

@Sija
Copy link
Contributor Author

Sija commented Dec 13, 2016

This PR will get outdated soon due to many occurrences of Slice(UInt8) in the code so it would be great to get it merged rather sooner than l8r :)

@RX14
Copy link
Contributor

RX14 commented Dec 13, 2016

Personally I think that alias is much more useful for naming union types. Calling it Bytes hides the fact that its a slice and you can call slice methods on it. You know it's a slice, I know it's a slice but its one more thing for a newcomer to learn.

@RX14
Copy link
Contributor

RX14 commented Dec 13, 2016

Using Slice.empty is good though.

@Sija
Copy link
Contributor Author

Sija commented Dec 18, 2016

@RX14 Learning about Bytes being an alias for Slice(UInt8) IMO is not too much to require from newcomers and afterwards makes for way smoother experience with reading code. I'd argue that learning that Slice(UInt8) is a Crystal way to handle byte slices is another requirement which, after you've learned about Bytes, becomes more clear.

@asterite any action here? :)

@RX14
Copy link
Contributor

RX14 commented Dec 18, 2016

@Sija Actually, Knowing Bytes is a Slice(UInt8) means that they can look up Slice and UInt8 in the docs, however Bytes has no documentation of its own.

@Sija
Copy link
Contributor Author

Sija commented Dec 18, 2016

@RX14 and what about this? There are links there to both: Slice & UInt8 :)

@RX14
Copy link
Contributor

RX14 commented Dec 18, 2016

Personally, my view on alias is the same as @asterite's view on aliasing methods: aliases mean more things to learn and they erode simplicity.

I think that the only valid use for alias is naming union types.

@Sija
Copy link
Contributor Author

Sija commented Dec 18, 2016

In my eyes Bytes as an alias is more like a composition of Slice with UInt8 being closer to an idiom than alias. Aliased methods OTOH are just different names for exactly the same set of properties.

@asterite asterite merged commit 44d0345 into crystal-lang:master Dec 18, 2016
Sija added a commit to Sija/crystal that referenced this pull request Dec 19, 2016
@Sija Sija mentioned this pull request Dec 19, 2016
asterite pushed a commit that referenced this pull request Dec 20, 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

5 participants