Why is this not possible: ```fun interface A { ...
# getting-started
Why is this not possible:
Copy code
fun interface A {
    fun foo(b: Int = 42) // Single abstract member cannot declare default values
Screenshot 2022-02-22 at 1.01.49 AM.png
since you written fun in the prefix of interface therefore it is causing issues
Yes, I want to keep the
modifier to use it as a
Copy code
A { }.foo()
I am not sure if it is allowed for interface
@Umar Ata
fun interface
is valid and defines functional interfaces
@hfhbd to be honest I don't know, but I guess it may have to do with the fact that it wouldn't know how to convert the interface into a function: it could be either a
() -> Unit
or a
(Int) -> Unit
, and I guess that ambiguity could cause problems during SAM conversions
@Joffrey What do you mean? The parameter of the body?
Copy code
A { i -> // this?

Even with default values in the definition (the interface), you still would need to declare the variables in the body, implementation. If you mean calling the (single) member
, the compiler is able to infer the correct overload, like its normal behavior with default values. Or do you mean this:
val a: (Int) -> Unit = A { }
? This does not compile, because even a
fun interface
does not inherit from
(and its
) by default.
I meant the very last part: using an instance of the fun interface where a function is expected. I thought SAM conversions were supposed to work in that case, but I guess I was wrong