https://kotlinlang.org logo
Title
o

orangy

06/22/2018, 8:56 PM
Extension delegating property should work
1
h

Hullaballoonatic

06/23/2018, 4:22 PM
so a property that delegates an extension? i didn't know that was a thing. can that be used to dynamically implement interfaces on instantiating a class?
h

Hullaballoonatic

06/23/2018, 5:11 PM
thanks!
i can't do delagation since i want to point to an interface property. not sure how extension property helps me here
i have an interface with a property called
scalingProperty
and i want to be able to implement this interface but allowing to name the property as something with context, like
volume
am i overcomplicating this? is that as easy as
var volume: Volume
    get() = scalingProperty
    set(v) {
        scalingProperty = v
    }
o

orangy

06/23/2018, 6:00 PM
yes, but
var YourInterface.volume : Volume
and the rest is as you wrote
You want to invoke it
instance.volume
, right?
h

Hullaballoonatic

06/23/2018, 6:00 PM
yes
i think i had it backwards
interface Liquid<T: ItemData> : Scale<Volume, T> {
    override var scalingProperty: Volume
        get() = volume
        set(value) {
            volume = value
        }
    
    var volume: Volume
}
o

orangy

06/23/2018, 6:13 PM
That would work too, of course, but I would make
scalingProperty
final, so that it wouldn’t be overriden further in hierarchy
h

Hullaballoonatic

06/23/2018, 6:14 PM
can't implement final in interface
o

orangy

06/23/2018, 6:15 PM
Ok, let me open an IDE 🙂
So, I was thinking along the lines that anything with
Volume
should have property `volume`:
interface Scale<TUnit, TData> {
    var scalingProperty: TUnit
}

interface Volume
object Barrel : Volume

var <TData> Scale<Volume, TData>.volume
    get() = scalingProperty
    set(value) {
        scalingProperty = value
    }

fun test(some: Scale<String, Int>, liquid: Scale<Volume, Int>) {
    some.volume = Barrel // error
    liquid.volume = Barrel // ok
}
✍️ 1
😄 1
h

Hullaballoonatic

06/23/2018, 6:25 PM
do you know if it is possible to dynamically implement varying interfaces on a class during instantiation?
o

orangy

06/23/2018, 6:29 PM
Like
object : Volume {}
?
Kotlin is statically typed language, so no dynamic subtyping
h

Hullaballoonatic

06/23/2018, 6:30 PM
ah, shucks
o

orangy

06/23/2018, 6:32 PM
Nope, that rulz 🤘
👍 1
:kotlin-flag: 1