Crystal: [RFC] Alias for Slice(UInt8)

Created on 2 Sep 2015  路  10Comments  路  Source: crystal-lang/crystal

Can an alias of Slice(UInt8) be added to reduce carpel tunnel?

Suggestions:

  • Bytes
  • Buffer
accepted stdlib

Most helpful comment

For me:

alias Bytes = Slice(UInt8)
alias Chars = Slice(Char)

# A string is a sequence of bytes that follow UTF-8 convention,
# so it's Bytes with a meaning, plus a lazily computed codepoints size
# (and maybe more information in the future, like a precomputed hash)
class String
end

We could even add alias Byte = UInt8 so alias Bytes = Slice(Byte) makes more sense.

All 10 comments

Hehe, we were talking about this with @waj the other day. We were thinking more of a syntax for slices, like UInt8* is Pointer(UInt8), but Bytes sounds good. We could also have Chars eventually.

Binary

Another variant would be Bytes

I like Bytes too

Because it's shorter and one usually says "read some bytes"... Maybe also "read binary data", but then we'd use BinaryData. So Bytes for me :-)

if Bytes then string should be called Chars. also Bytes associated in my mind with [] of UInt8. But with current String, i thin Binary is ok.

For me:

alias Bytes = Slice(UInt8)
alias Chars = Slice(Char)

# A string is a sequence of bytes that follow UTF-8 convention,
# so it's Bytes with a meaning, plus a lazily computed codepoints size
# (and maybe more information in the future, like a precomputed hash)
class String
end

We could even add alias Byte = UInt8 so alias Bytes = Slice(Byte) makes more sense.

Should this be closed?

I believe that adding an alias for only Bytes here is an incomplete solution. It'd be more consistent and elegant to introduce

alias Byte = UInt8
alias Bytes = Slice(Byte)
alias Chars = Slice(Char)

to make it possible to rewrite methods like IO#read_byte, IO#write_byte in a unified way

#read_byte: Byte | Nil (counterpart of #read_char : Char | Nil)
#write_byte(byte : Byte)

This was for Bytes only, please open a new RFC for other aliases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

costajob picture costajob  路  3Comments

asterite picture asterite  路  3Comments

asterite picture asterite  路  3Comments

nabeelomer picture nabeelomer  路  3Comments

lbguilherme picture lbguilherme  路  3Comments