Hi all,
Lets say I have a:
data class Person(val firstName:String, val lastName:String)
val p = Person("Dart", "Vader")
and say I want to make a copy of ‘p’ but with lowercase latters:
val p1 = p.copy(firstName = p.firstName.toLowerCase(), lastName = p.lastName.toLowerCase())
But what if I want to make a universal function or framework which does not know about particular type A and names of its fields? I’d like to have something like:
p.map { k, v ->
v.toLowerCase()
}
Another interesting case (but having vital difference from previous):
data class Vector<T out> (val x:T, val y:T)
val vDouble = Vector(1.0, 2.0)
val vInt = toIntegerVector(vDouble)
fun toIntegerVector(v:Vector<Double>) = Vector<Int>(toInt(v.x), toInt(v.y))
to convert vector of doubles to vector of integers, of course, we might implement a specific conversion function, but if would have a possibility to map over type wrapped by other it would be far more generic and provided an additional way to make generalized frameworks. Example:
data class Vector<T out> (val x:T, val y:T)
val vDouble = Vector(1.0, 2.0)
val vInt = vDouble.mapT(::toInt)
This feature may sound strange and too specific, but, in some languages this feature is a corner stone (see Functor, Haskell).