With the following code ```public fun interface Ex...
# random
e
With the following code
Copy code
public fun interface Example {
    public fun execute(str: String)
}

public class MyClass(example: Example = Example { it + "123" })
What's the reason I can't simply write
Copy code
public class MyClass(example: Example = { it + "123" })
But I have to specify
Example { ... }
?
h
You could also have another fun interface extending Example.
e
Couldn't it be treated as an implicit
Example
?
h
I don't think so:
Copy code
fun interface A {
    fun a(): String
}

fun interface B : A {
    override fun a(): String {
        return "ASDF + ${b()}"
    }

    fun b(): String
}

fun main() {
    val a: A = A {
        "Hello A"
    }
    println(a.a())
    val b: B = B {
        "Hello B"
    }
    println(b.a())
    val c: A = B {
        "Hello C"
    }
    println(c.a())
    if (c is B) {
        println(c.b())
    }
}
e
But in the context of a default parameter value I don't see how an extention to A would matter
h
But which type should the compiler prefer? Why A/Example? B would be okay too.
e
I mean, in that case it's ok to be specific, but if the parameter is of type A, then the default implementation should be an extension to A. I'm just half complaining because it seems redundant information.
k
@hfhbd in your example, if we had a function that takes a parameter of interface `A`:
Copy code
fun foo(a: A) {
    a.a()
}
then the compiler allows us to pass an implicit
A
instance, even if it was actually a
B
. All of these lines will compile ok:
Copy code
foo { "Hello" }
val b: B = B { "Hello" }
foo(b)
foo(B { "Hello" })
So I guess the OP's question is why can you pass an
A
implicitly as a function parameter, but not as a default parameter?
✔️ 1
h
Okay, got it now, but your sample is only valid with trailing lambdas:
Copy code
val a: A = { // error: required A, found: () → String
  "Hello"
}
So this syntax "should" be supported too and not only for default parameters.
e
Yes! Absolutely, it should be supported wherever we have a fun interface.
It's a minor syntax annoyance, but with long interface names it's super annoying.
k
You could suggest it in #CQ3GFJTU1