https://kotlinlang.org logo
#getting-started
Title
# getting-started
e

Edward Muller

12/09/2023, 12:29 AM
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

Daniel Pitts

12/09/2023, 1:06 AM
[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

Edward Muller

12/09/2023, 1:10 AM
yeah, not really.
d

Daniel Pitts

12/09/2023, 1:10 AM
Furthermore, both of the approaches you wrote are practically the same implementation, so you get no performance benefit from either one of them.
e

Edward Muller

12/09/2023, 1:11 AM
👍
d

Daniel Pitts

12/09/2023, 1:11 AM
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

Arjan van Wieringen

12/10/2023, 7:53 PM
Honest question: which situations do you have that this actually matters?
d

Daniel Pitts

12/10/2023, 7:57 PM
You mean typealiasing lambdas?
Often when designing frameworks and reusable libraries, rather than any application code.
a

Arjan van Wieringen

12/11/2023, 6:54 AM
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

CLOVIS

12/11/2023, 9:24 AM
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

Arjan van Wieringen

12/11/2023, 9:40 AM
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