Is there an easy way to create a Set<Int> fr...
# getting-started
t
Is there an easy way to create a Set<Int> from an OpenEndRange<Int>? I wanted to just do
setOf(*(0..<15))
, but the spread operator doesn't like using a Range? (in my case, the range is programatically created, rather than a literal as shown there)
k
If it's actually an
IntRange
you can use
range.toSet()
. It doesn't work for an
OpenEndRange<Int>
t
Why is that?
s
wait, no
Copy code
(0..<15).toSet()
works just fine
kodee happy 1
k
Because there's no guarantee that an
OpenEndRange<T>
contains a finite number of Ts. It does for
Int
, but that's just a special case. What would you say
("Alpha"..<"Zulu").toSet()
should contain?
(0..<15).toSet()
works fine because
(0..<15)
is an
IntRange
, which implements the
OpenEndRange<Int>
interface, but importantly it also implements
Iterable
.
t
thanks
s
okay, well, when defining a range containing a finite number of elements with a defined ordering, such as
Int
, like Travis asked,
.toSet()
works.