kotlin "newishly" person Q given ```typealias Req...
# getting-started
e
kotlin "newishly" person Q given
Copy code
typealias RequestGroupingKey = Triple<String, String, String>
Which one of this is more lighter weight & more performant?
Copy code
val RequestGroupingKey.thing: String
    get() = this.first
or
Copy code
fun RequestGroupingKey.thing(): String = this.first
or is there something else I should be doing like a data class?
d
[deleted] Revising my answer....
You should almost definitely use a data class instead.
Copy code
data class RequestGroupingKey(val thing: String, val ama: String, val bob:String)
gives you a better semantic type. One of the downsides to what you wrote is that
RequestGroupingKey
is simply an alias for
Triple<String, String, String>
, which means anyone with any
Triple<String, String, String>
can call
.thing
on it. Which I'm assuming was not the intent.
plus1 2
e
yeah, not really.
d
Furthermore, both of the approaches you wrote are practically the same implementation, so you get no performance benefit from either one of them.
e
👍
d
Pretty much the only time I use
typealias
is for callback signatures:
Copy code
typealias SomeEventListener = (SomeEvent)->Unit
1
Even then, it's mostly useful only when the signature itself is more complex, and is used in many places.
a
Honest question: which situations do you have that this actually matters?
d
You mean typealiasing lambdas?
Often when designing frameworks and reusable libraries, rather than any application code.
a
I mean the performance implications. When are you entering a situation when this really starts to matter? And how much of a difference is it?
c
I mean the performance implications.
Which implications are you thinking of? Typealiases are purely syntactic sugar, they do nothing, so they do not have any performance difference compared to using what they refer to directly.
a
I know, but I am asking OP. He specifically asked which one was better for performance and I am wondering what kind of ultra performant requirements there are that something like this would make sense