More ranges: other than the standard `[a..b]` we g...
# language-proposals
m
More ranges: other than the standard
[a..b]
we get with the
..
constructor (and even the
until
keyword, which is still a
[a..b]
range. They don't need to be progressions.
example 1: Being able to do the following is nice
Copy code
when(value){
    in null until 4 -> "value1"
    in 4 until 8 -> "value 2"
    in 8 until null -> "value 3"
}
g
Do you mean
null
means in this case that range is not limited on one side? I understand it’s just to illustrate idea, but using null in this case is probably extremely confusing, imagine this code:
Copy code
val from = if (Random.boolean) null : 1
from until 4
m
I don't see what your code is supposed to illustrate, but it was an example yes. Although locally I have made an openrange where null represents the "absence of a bound", which makes sense IMHO, but I'm willing to forfeit that concept if we get proper ranges in return 😉
g
Illustrate that you can have nullable variable which creates 2 completely different ranges and it implicit and cannot be catched by compiler properly and very error prone
🙌 1
m
(until 4) or (..4) is fine by me as well, the example was an example of when it would be nice to have, the intrinsics of the implementation can be forgotten. From all the possibly mathematical ranges, we only have one.
g
< 4
also works for this check, of cours you cannot use value implicitly, but at least it doesn’t require creation of new range I mean if we concidering
when
as use case for it. So if main goal is when, maybe we indeed should discuss possible when syntax changes, to support more use cases like in/equals
m
another example where an open range would be nice:
Copy code
when(value){
    in 3.2 until 3.3 -> "value1"
    in 3.3..3.5 -> "value1"
}
This ofcourse giving a syntax error now
k
what's wrong with
<
>
? is opened range till MIN/MAX value?