tipsy
01/23/2020, 8:45 PMtypealias Address = String
typealias Age = Int
when i see these typealiases used in signatures, i always assumed they're classes
is there a guideline on this? i personally find it very confusing, but it might just be because it's unfamiliarnfrankel
01/23/2020, 8:53 PMtypealias Cache = Map<String, Any?>
tipsy
01/23/2020, 9:02 PMDaniel
01/23/2020, 9:03 PMnapperley
01/23/2020, 9:52 PMtypealias GrabFocusSlot = CFunction<(widget: CPointer<GtkWidget>, userData: gpointer) -> Unit>
Mike
01/23/2020, 10:27 PMdata class Address(val value:String)
or even better, an inline class
will ensure the type is enforced where it is used.
typealias
is good for giving a descriptive name to a complex type, or a function/Lambda signature, but in the bytecode, the typealias
name will not exist.napperley
01/23/2020, 11:21 PMcodeslubber
01/23/2020, 11:33 PMDico
01/23/2020, 11:56 PMinline class
is "even better" than a data class. Only use inline class
if you are aware of its behaviour. The only use case for inline class
is performance. It is very very very easy to write code that will constantly box and unbox inline classes, leading to detrimental performance to a data class. @Mike
This happens when using the type as nullable, as a generic type, and even when comparing two values of an inline class for equality (non-unsigned ints at least, kotlin ~1.3.50)Dico
01/23/2020, 11:57 PMDico
01/23/2020, 11:57 PMinline class
is bad, just be cautious especially because of its experimental state (which is likely to be there for a long time)napperley
01/24/2020, 12:28 AMcodeslubber
01/24/2020, 1:19 AMStephan Schroeder
01/24/2020, 11:49 AMtypealias Username = String
typealias Password = String
the compiler won’t complain if you assign a Username instance to a Password field, e.g. if you mix up the order in a method signature
fun login(username: Username, password: Password) : AccountHandle
but with Inline Classes it will!
inline class Username(val value: String)
inline class Password(val value: String)
@Dico So I’d say there is more to Inline Classes than performance.
And I always thought Inline Classes were a mere compile-time construct, so there wouldn’t be any Boxing/Unboxing involved (the runtime type would be String). Did I get this wrong??
Final words: I myself wait for Inline Classes leaving experimental status before using them. Hopefully that’ll happen in Kotlin 1.4 which should come out soon (The KotlinConf opening keynote pinpointed the release of 1.4 to Spring 2020).Dico
01/24/2020, 3:35 PMinline
modifier (add data
for equals checks). The inline
modifier adds some restrictions and changes the code emitted by the compiler, the goal of which is to improve performance. Therefore, the only underlying reason to ever use inline
is for improved performance. There is no other reason. As I explained though, it is easy to use it in a way that makes it detrimental to performance, and the compiler does not help you in any way to detect that.Stephan Schroeder
01/24/2020, 3:39 PMdata class
and than inline class
is more performant, but I’m coming from just just the basic type (String in my example) and then using inline class
will be more type safe (but exactly equal in performance) 😉Dico
01/24/2020, 3:41 PMStephan Schroeder
01/24/2020, 3:46 PMdata class
. I didn’t even think of that possibility before 😅 So yeah, inline class
leaving experimental will be a good day for me 🙂codeslubber
01/24/2020, 4:01 PMDico
01/24/2020, 4:25 PMcodeslubber
01/24/2020, 4:51 PMDico
01/24/2020, 4:59 PM