user2340612
user2340612

Reputation: 10703

Kotlin @FunctionalInterface compiles with multiple abstract methods

When trying to compile a Java @FunctionalInterface having more than 1 non-abstract method a compilation error is raised.

However, when doing the same in Kotlin, no errors or warnings are raised, i.e. the following Kotlin interface compiles successfully:

@FunctionalInterface
interface Foo {
    fun foo()
    fun foo(params: Map<String, String>)
}

Is this the intended behaviour or a bug in the Kotlin compiler?

Please note that the generated bytecode for the above Kotlin snippet is equivalent to the following Java snippet (which – correctly – doesn't compile):

@FunctionalInterface
// metadata omitted
public interface Foo {
   void foo();
   void foo(@NotNull Map var1);
}

Upvotes: 4

Views: 119

Answers (1)

user2340612
user2340612

Reputation: 10703

Issue KT-25512 has been submitted to JetBrains's issue tracker (by another user) to report the fact that the compiler misbehaves when @FunctionalInterface is applied to a non-SAM interface, and as of 10 Feb 2019 the issue is still open with no activity.

EDIT

Not really like for like, but starting from Kotlin 1.4 the language defines a new syntax for defining functional interfaces:

fun interface Foo {
    fun foo()
}

The compiler will complain if a fun interface isn't a functional interface:

fun interface Foo {
    fun foo()
    fun anotherFoo()
}

gives an error:

Fun interfaces must have exactly one abstract method

Upvotes: 2

Related Questions