gsala
11/05/2018, 4:05 PMUser requires a UserID to be constructed, it shouldn't be allowed to be constructed with any IntDJ Mitchell
11/05/2018, 4:10 PMval intId = 5
val user = User(intId)
since intId is an Intgsala
11/05/2018, 4:12 PMDJ Mitchell
11/05/2018, 4:13 PMdata class User(
val userID: UserID,
val email: Email,
val minimumCostToShip: Dollars
) {}
(its contrived but UserID, Email, Dollars are all inline classes)
Constructing a User for testing or something similar becomes much more verbose, having to manually wrap the type each time.DJ Mitchell
11/05/2018, 4:13 PMDJ Mitchell
11/05/2018, 4:14 PMDJ Mitchell
11/05/2018, 4:14 PMMike
11/05/2018, 4:14 PMDJ Mitchell
11/05/2018, 4:15 PMuser.id == product.id) but does so in a way that makes the callsite of using them just a little bit less cumbersomeDJ Mitchell
11/05/2018, 4:17 PMDJ Mitchell
11/05/2018, 4:17 PMDJ Mitchell
11/05/2018, 4:17 PMInt, whereas the Swift protocols are really just compiler magic to lift an Int literal into the type, purely for code clarity at the callsite of creating a User
With your fun User I could do something like this:
fun validateUserInfo(phoneNumber: Int): User? {
return User(phoneNumber)
}marstran
11/05/2018, 4:21 PMinline class Age(val age: Int)
inline class Height(val cm: Int)
data class Person(val name: String, val age: Age, val height: Height)
val age: Int = 20
val height: Int = 175
// Oops, mixed height and age parameters.
Person("John", height, age)Mike
11/05/2018, 4:21 PMDJ Mitchell
11/05/2018, 4:22 PMmarstran
11/05/2018, 4:23 PMDJ Mitchell
11/05/2018, 4:23 PMliterals, so once you assign it to a variable, you can no longer pass it to the initializer without wrapping it inside the inline classMike
11/05/2018, 4:23 PMDJ Mitchell
11/05/2018, 4:23 PMPerson("John", 120, 25)
but not Person("John", height, age)marstran
11/05/2018, 4:24 PMDJ Mitchell
11/05/2018, 4:24 PMDJ Mitchell
11/05/2018, 4:24 PMgsala
11/05/2018, 4:24 PMDJ Mitchell
11/05/2018, 4:26 PMDJ Mitchell
11/05/2018, 4:26 PMRuckus
11/05/2018, 4:29 PMDJ Mitchell
11/05/2018, 4:31 PMval a: Double = 5 would that work? Or would I have to say 5.0?DJ Mitchell
11/05/2018, 4:33 PMDJ Mitchell
11/05/2018, 4:34 PMfun foo(id: Int?) {}
val id = 5
foo(id) // in Swift, even though id is Int rather than Int? it will automatically lift it into an optional IntDJ Mitchell
11/05/2018, 4:35 PMdalexander
11/05/2018, 4:36 PMval a: Double = 5.0DJ Mitchell
11/05/2018, 4:37 PMgsala
11/05/2018, 4:39 PMInt is a subtype of Int? so it's not lifting the type but rather using type inheritanceDJ Mitchell
11/05/2018, 4:40 PMgsala
11/05/2018, 4:41 PMdalexander
11/05/2018, 4:42 PMFoo is a union of Foo and null. And Gerard found the post I was looking for =pDJ Mitchell
11/05/2018, 4:42 PMOptional is implemented more akin to Arrow's Option type, as a sum type (enum in Swift, sealed class in Kotlin defined) at the language level so its more like type-liftingDJ Mitchell
11/05/2018, 4:43 PMRuckus
11/05/2018, 4:43 PM