Custom interface delegate?

#1

Is it possible to define your own delegate for interface delegation?

I know I could just do:

interface Foo {
    val aaa: String
    val bbb: Int
    val ccc: Date
}

class Bar() : Foo {
    val aaa: String by myDelegate()
    val bbb: Int by myDelegate()
    val ccc: Date by myDelegate()
}

but that would get tedious pretty soon if you had to do this with many classes with many properties. Wouldn’t it be much nicer to be able to do?:

class Bar(myDelegate) : Foo by myDelegate()

Edit: re-reading I realize it might not be obvious why I want this. Basically I want the by Map functionality on entire interfaces, so I´d want something like this:

class Bar(map: Map<String, Any>) : Foo {
    val aaa: String by map
    val bbb: Int by map
    val ccc: Date by map
}

to be turned into:

class Bar(map: Map<String, Any>) : Foo by map

but that doesn’t work, but I could implement it if I could implement a custom delegate for interfaces.

#2

The problem I see that the syntax you describe is already used for interface delegation.
Maybe it can be extended so that Map can “represent” any interface/open class or at least all properties defined in them.

#3

Indeed, but perhaps the compiler can be smart about it and say “the type of the delegate isn’t correct but it has a getValue() method so I’ll use that instead”?

But yeah, if they’d extend the current interface delegation to accept Maps then that’d fix my current problem too :wink:

I just like to be able to implement everything myself instead of relying on too much compiler magic if at all possible.