https://kotlinlang.org logo
#stdlib
Title
m

mbonnin

01/13/2021, 4:27 PM
Is there a specific reason why
Set<T>.map {}
returns a
List
and not a
Set
?
z

Zach Klippenstein (he/him) [MOD]

01/13/2021, 4:34 PM
You can look at the definition of the extension function, I think it’s defined on
Iterable
so
List
is the return type that makes the most sense given the generic receiver.
m

mbonnin

01/13/2021, 4:38 PM
Right, there's no
Set<T>.map {}
...
Only
Iterable<T>.map {}
I thought maybe there were considerations that a List is faster than a Set
r

Ruckus

01/13/2021, 4:44 PM
Also, even if it was defined on
Set
, it wouldn't necessarily make sense to return a set, as we don't know if the mapping function will return unique values. So dropping data when mapping to a set should be explicit.
☝️ 8
☝🏽 1
m

mbonnin

01/13/2021, 4:44 PM
@Ruckus right, makes sense
Well, it could return a
Set
of a different size I guess
z

Zach Klippenstein (he/him) [MOD]

01/13/2021, 4:48 PM
A set can’t contain duplicate values, by definition
m

mbonnin

01/13/2021, 4:49 PM
Mmm right, and since the order or iteration is not defined, what data is going to be dropped would not be defined either
j

jw

01/13/2021, 4:51 PM
If you want a set, use mapTo and supply one
👍 6
2 Views