Is it legit to have a dedicated interface containi...
# getting-started
e
Is it legit to have a dedicated interface containing a function with a lambda with a receiver to be implemented by third parts?
Copy code
interface Action {
//    interface DSL Builder
    operator fun<T> invoke(block: T.() -> Unit)
}
Because I cant seem to properly override in the implementation..
r
Copy code
class ActionImpl : Action {
    override fun <T> String.invoke(block: T.() -> Unit) {
        // Do something
    }
}
Just a standard implementation, unless I misunderstood your question.
e
If I try:
Copy code
object V2 : Action {
    operator fun invoke(block: Builder.() -> Unit) {..}
    class Builder
I get on
object V2
Object 'V2' is not abstract and does not implement abstract member public abstract operator fun <T> invoke(block: T.() → Unit): Unit defined in kyge.Action
while on the function itself:
Accidental override: The following declarations have the same JVM signature (invoke(Lkotlin/jvm/functions/Function1;)V):
public abstract fun <T> invoke(block: T.() → Unit): Unit defined in kyge.actions.checkout.V2
public final operator fun invoke(block: checkout.V2.Builder.() → Unit): Unit defined in kyge.actions.checkout.V2
Ps: it isn't an extension function anymore
r
You may want to move the generic to
Action
, not
invoke
.
Copy code
interface Action<T> {
    // interface DSL Builder
    operator fun invoke(block: T.() -> Unit)
}

object V2 : Action<Builder> {
    override fun String.invoke(block: Builder.() -> Unit) {
        // Do something
    }

    class Builder
}
Otherwise
invoke
needs to be able to handle any generic value
e
ah nice
thanks
👍 1
one thing though, I'm trying to move the
invoke
function inside the
Action
interface, but I don't know how I can call
block
from that.. I'm thinking what the best design could be
maybe adding a
val t: T
in the interface?
r
I'm not sure what you're asking. Maybe show how you want to use it, and we can work backward from there?
e
I'm writing a DSL helper to generate github action yaml files. The i-th action, such as uploadArtifact, should just declare the builder Then the implemented
Action<T>
interface would initialize the yaml writing