You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Range#size raises a compile time error when either the begin or end type of the range are Nil. This method overrides Enumerable#size.
Raising at compile time effectively makes this method undefined for Range(T, Nil) and Range(Nil, T) and subsequently for Enumerable(T) and Enumerable(Nil), respectively. This breaks the expectation that a subtype inherits all methods of a parent type. When a range type with Nil parameter is instantiated anywhere in a program, it makes it impossible to call Enumerable#size because then not all inheriting types implement that method.
0..
[1, 2, 3].as(Enumerable(Int32)).size # Error: Can't calculate size of an open range
There is no way to work around that, except changing the argument type of Range from Nil (it can be nilable, which raises at runtime when the value is nil).
To fix this, Range#size must not raise at compile time.
The existing runtime error for nil values should be sufficient.
This compile time error was introduced in #8829.
The issue was first reported in Sija/debug.cr#7
The text was updated successfully, but these errors were encountered:
Isn't there a way to have this raise exclusively for Range? Having this checked at compile time makes it easier to catch accidental footguns and you are guaranteed to not run into this further down development, whereas raising at runtime does the opposite.
Range#size
raises a compile time error when either the begin or end type of the range areNil
. This method overridesEnumerable#size
.Raising at compile time effectively makes this method undefined for
Range(T, Nil)
andRange(Nil, T)
and subsequently forEnumerable(T)
andEnumerable(Nil)
, respectively. This breaks the expectation that a subtype inherits all methods of a parent type. When a range type withNil
parameter is instantiated anywhere in a program, it makes it impossible to callEnumerable#size
because then not all inheriting types implement that method.There is no way to work around that, except changing the argument type of
Range
fromNil
(it can be nilable, which raises at runtime when the value isnil
).To fix this,
Range#size
must not raise at compile time.The existing runtime error for
nil
values should be sufficient.This compile time error was introduced in #8829.
The issue was first reported in Sija/debug.cr#7
The text was updated successfully, but these errors were encountered: