I'm trying to find a way to bring multiple values ...
# announcements
r
I'm trying to find a way to bring multiple values into scope with inheritance. Currently this is what I'm doing:
Copy code
interface A {
    val prop1: ...
    val prop2: ...
    ...

    companion object : A {
        ...
    }
}

interface B : A {
    val prop3: ...
    ...

    companion object : B, A by A {
        ...
    }
}
Is there a way to do this without the weird interface / companion construct?
It would be awesome if there was something more along the lines of:
Copy code
object A {
    val prop1: ...
    val prop2: ...
}

object B : A {
    val prop3: ...
}
But you obviously cannot extend an object from an object.
h
class
?
e
Does it need to be part of the interface or you just want to bring values into scope for internal use?
r
@hho Not really what I'm looking for. My only purpose is to be able to bring those values into scope in certain places:
Copy code
// In context regarding the types
with (A) {
    doSomething(prop, ...)
}
@edrd I just need to bring the values into scope, but It's user facing, otherwise I'd just use an uglier syntax.
Let me show a more fleshed out example. Just a sec.
h
I meant, you can use a class for
A
and only your "last hierarchy level" needs to be an
object
. That way, you can inherit how deep you want (Example). But I'm probably not fully understanding what you're after…
r
No,
A
needs to be an object too. The user needs to be able to work within the context of
A
if they want, but I also want
B
to inherit the context of
A
.
Untitled.cpp
It's a DSL for setting properties. The user can define whatever properties they want, but there are also various sets of predefined properties (some that depend on others) that the user should be able to bring in to scope if they wish.
The idea is to use it for "theming" (more accurately, theme overriding):
Copy code
PlatformTheme
BuiltInTheme1: PlatformTheme
BuiltInTheme2: PlatformTheme
The user could then choose to use the base platform theme, any of the built in themes, or a custom theme which could either be fully custom or derived from an existing theme. However, the "theme" is essentially just a list of predefined "properties" the user can use. The actual implementation is a fair amount more complicated, with more than just a
Values
context, so I unfortunately cannot just use a simple class structure and throw the
Values
in there.
s
How about composition instead of inheritance. B being a wrapper layer with its own props, and other others which are delegated to A
r
That would mean duplicating all of
A
in
B
. My goal is to get rid of all the duplication I'm currently doing in the companion, not move it somewhere else.
If only we had multiple receivers, I could restructure the entire thing to be easier to maintain and so much cleaner for the user.
e
It's coming soon... 🙂
r
Oh? Do you have the inside scoop on that? 🙂
h
It's at least on the roadmap: https://youtrack.jetbrains.com/issue/KT-42435
r
Indeed. I was hoping there might be more to "coming soon..." than what could be found there.
e
@Ruckus the design proposal was published today: https://github.com/Kotlin/KEEP/issues/259
r
Inside scoop indeed 😀 Thanks!
😁 1