`Slice(T)#size` should be `UInt64`, `UInt32`, or `LibC::PtrdiffT`

It makes no sense for #size to be negative, so that should obviously be unsigned at the very least.

  • For compatibility with the most platforms (and still an improvement of one bit over the current implementation), it could be a UInt32. This provides the least opportunity for compile or runtime bugs on any 32+ bit platform, at the expense of Slices being at most 1 << 31 items long.
  • For compatibility with the most popular platforms and greater versatility on those platforms, it could be a UInt64. I don’t think this is the best idea because it could lead to weird bugs on 32 bit systems.
  • For compatibility with the widest array of platforms and operating systems, LibC::PtrdiffT could be chosen. This could result in some occasional compile-time or runtime errors, but for most cases things would work smoothly.

I will gladly implement this and submit a PR, but I’d like some opinions on which option is the best for Crystal before I do.

1 Like

There is already an open issue: https://github.com/crystal-lang/crystal/issues/3209

Unfortunately, the topic is more complex than just changing to any UInt type. See previous discussion for details.