Leon K
02/23/2020, 1:01 PMraulraja
02/23/2020, 5:48 PMLeon K
02/23/2020, 5:51 PMfun foo(x: String | Int) = when(x) {
is String -> {...}
is Int -> {...}
}
? that sound's awesome!raulraja
02/23/2020, 5:52 PMLeon K
02/23/2020, 6:00 PMSyntax<A>
part? what does that Syntax
interface do / mean?raulraja
02/23/2020, 6:01 PMLeon K
02/23/2020, 6:12 PMraulraja
02/23/2020, 6:16 PMLeon K
02/23/2020, 6:27 PMTwitterHandle("@adminxxxxxxxxxxxxxxxxxxxxxxxx")
, check's it with the validate function and then knows it's not a valid TwitterHandle, thus the code does not compile? That sounds pretty cool!
But how does this work with non-literal values?
I see you have these examples:
val runtimeAdminWillBeNull: TwitterHandle? = admin
//null
val runtimeWhateverIsValidated: TwitterHandle? = whatever
//TwitterHandle("@whatever")
..
Am I correct in understanding it the following way:
if you provide a literal, the compiler can know that your value is either valid or not, and validate the type at compile-time. If you give it a value or otherwise a "calculation", It cannot compile-time verify and thus returns a nullable type?raulraja
02/23/2020, 6:34 PMLeon K
02/23/2020, 6:39 PM@Proof
function that converts from String -> TwitterHandle?
(or one that converts into Maybe<TwitterHandle>
, etc) which is then used to generate the inline class instance?
what would happen if i did TwitterHandle(admin)
(put a non-literal value into the constructor)?raulraja
02/23/2020, 6:56 PMTwitterHandle.from
or one of the validation methods in the refined interface, still under designLeon K
02/23/2020, 7:04 PMraulraja
02/23/2020, 7:07 PM