4 ms·
Hmm, this does seem like a pretty tasteful design in many ways. The declaration-site variance for generics is a pretty good idea, but is still inadequate to de
by ScottBurson 10y ago
Hmm, this does seem like a pretty tasteful design in many ways.
The declaration-site variance for generics is a pretty good idea, but is still inadequate to deal with functional collections. (I use the term "functional" as opposed to simply "immutable": an immutable collection has no update operations at all, while a functional collection has functional update operations: for example, given a set S and an element X, to compute a new set whose elements are those of S together with X (S ∪ {X}). This usage is certainly not universal, but the distinction needs to be drawn somehow.)
Anyway, the point is this. Kotlin has in its collections API
interface Set<out E> ...
where the use of 'out' says that given types A and B where B is a subtype of A, 'Set<B>' is a subtype of 'Set<A>'. But then if you wanted to add a 'with' operation as described above:
interface FunctionalSet<out E> : Set<E> {
fun with(E elt)
}
you can't do it, because type parameters marked with 'out' can't be used for method parameters.
- lmm 10y agoThe correct signature for that method is something like (don't know whether this is the syntax) fun with<F super E>(F elt)
- ScottBurson 10y agoI guess you mean (note the return type) fun with<F super E>(elt: F): FunctionalSet<F> That's an interesting suggestion, but I don't see any indication that Kotlin provides for type lower bounds in generic function constraints. (It does have type upper bounds; see the bottom of this page: http://kotlinlang.org/docs/reference/generics.html http://kotlinlang.org/docs/reference/generics.html).
- deleted 10y ago[deleted]