``` typealias UserId = String class User(val id: U...
# language-proposals
m
Copy code
typealias UserId = String
class User(val id: UserId)
val myString: String = "blabla"
User(myString) // wouldn't compile
b
How would this be advantageous over a wrapper- or sub-class?
m
Wrapper as in
class UserId(val value: String)
?
b
Yes
m
Almost none. Except for less memory use.
And maybe if you have it serialized to json, you don't need to write custom converters to skip one "layer" and send / receive id directly as string.
b
Maybe. Sounds like it'll lead to confusion. "I can assign a String to a var of type UserId, but I can't pass a String to a function that takes one?"
m
In real code you would probably almost never had a case of assigning String constant to a var/val.
So if you have a
ClientId
instead of
String
or
Long
and if you have it in
Purchase
table as FK and
Client
table as PK, you will never pass
ProductId
(also
String
or
Long
) to function
findClientById(ClientId)
, which you get from
Product
.
b
I see the need, but I can't imagine it'd ever work intuitively. If anything, it will just make the language more difficult to use. There would have to be very consistent rules about what will work where and what won't. Also these would break my system where I have an abstracted set of aliases like:
Copy code
typealias Int64 = Long
typealias Integer = Int64
so I'd now no longer be able to pass a
Long
literal to a function that takes an
Integer
? Or something like that?
s
Maybe there could be a casting mechanism?
I've thought about your newtype for entity id, which is just an int... But at some point you need to switch over
☝️ 1