Came across a situation where it would be handy to...
# language-evolution
n
Came across a situation where it would be handy to create a value type that accepted multiple values. For instance Ncurses has the concept of colour pairs where each pair comprises of a id, background colour, and foreground colour. Currently Kotlin's experimental inline classes only allow a single value which limits where inline classes can be used 🙁. Would be nice to do something like this:
Copy code
inline class ColourPair(val id, val backgroundColour: Int, val foregroundColour: Int) {
    fun generatePairValue(): Int {
        // ...
    }
}
m
Why is a class/data class not useful here? What are you hoping 'inline class' with multiple values will mean or will have the compiler do?
2
n
Avoid unnecessary resource overhead with a class/data class (eg memory usage).
☝️ 1
m
Inline classes provide Strong Typing, but once you get to bytecode, all you see is the type of the one property (at least I believe that's what it does), so it gets rid of the overhead. I'm not sure how your scenario would work. You have three values that are related, unless you want the three to be inlined to the result of
generatePairValue
. Is that what you want? But if you still want access to the properties too, then I don't see how
inline
could possibly work. What am I missing here?
d
What you are looking for, inlining multiple fields as a single data structure, is currently not supported by the JVM. It is being experimented on as part of Project Valhalla (http://cr.openjdk.java.net/~jrose/values/values-0.html), and as soon as it's implemented for Java, I'm sure Kotlin support will follow.
👍 1
j
@Mike Same doubt here, if the OP still wants access to all properties, I don't think anything could be inlined, so I don't see where an
inline
class would come in (as long as project Vlahalla doesn't make it to the JVM). However, if other properties aren't needed, then there is already the option of providing the 3 values to a secondary constructor, and the computed value as the single inline class property:
Copy code
inline class ColourPair(val computed: Int) {
    
    constructor(
        id: Int,
        backgroundColour: Int, 
        foregroundColour: Int
    ): this(TODO("computation here"))
}