September 2023
Intermediate to advanced
470 pages
11h 12m
English
We discussed algebraic data type Union Types. Now, let’s have another look at them in a more theoretical context.
In computer languages, the product of two types is a tuple with those two types, like Pair in Kotlin. Data classes can be considered products of types as well. It’s easy to see that, for example, a data class with a string and an integer is equal to a Pair<String, Int> up to an isomorphism.
| | data class User(val name: String, val id: Int){ |
| | constructor(pair: Pair<String, Int>): |
| | this(pair.first, pair.second) |
| | |
| | fun toPair(): Pair<String, Int> = name to id |
| | } |
| | |
| | val userPairIso: (Pair<String, Int>) -> Pair<String, Int> = |
| | isomorphism(::User, User::toPair) |
This perspective ...
Read now
Unlock full access