https://kotlinlang.org logo
#feed
Title
# feed
d

Dave Leeds

08/29/2018, 12:57 PM
Hello everyone! I’m super excited to share a new article - if you’ve been wondering about Kotlin’s new experimental inline classes feature, this one’s for you! https://typealias.com/guides/introduction-to-inline-classes/
👍 27
🔥 6
metal 5
1
j

jmfayard

08/29/2018, 8:23 PM
Are there patterns to work around the fact that we can do no validation in the constructor? Like if I have an
inline class Email
, I can define
fun String.asEmail() : Email? // null if invalid
d

Dave Leeds

08/30/2018, 1:15 AM
@jmfayard As currently implemented, I believe there's no fool-proof way to ensure validation happens up front, because Java call sites could get away with (for example) passing the underlying value as an argument to a Kotlin function without actually ever using the inline class type (e.g., pass a
String
instead of an
Email
). If we could choose to not allow Java to call Kotlin functions that have an inline class as a parameter (that is, ensure these functions can only have Kotlin callers), then
init
blocks and private constructors become a possibility. For more information on this possible approach, there's a ticket you can follow here: https://youtrack.jetbrains.com/issue/KT-25461
2 Views