# Enhanced interface implementation

**URL:** <https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309>\
**Category:** Uncategorized\
**Created:** [March 12, 2024, 10:08am UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309 "2024-03-12T10:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phill](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/phill/32/8551_2.png) [@phill](https://discuss.kotlinlang.org/u/phill)\
**Post date:** [March 12, 2024, 10:08am UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309/1 "2024-03-12T10:08:53Z")

</div>

Functional interfaces are great - being able to declare

```auto
fun interface AnInterface {
    fun aFunction()
}

// sometime later
AnInterface {

        }

```

really clarifies the code. The ability to just declare the instance of an interface is very concise.  
Why when we have more than one function to we need the syntax

```auto
    object: AnotheInterface {
    }

```

instead of

```auto
    AnotheInterface {
    }

```

It is just a small point, but it seems anomalous. I know the context within the braces is different, but I don’t think that would create confusion.

---

<div class="post-metadata">

**Author:** ![kylooh](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kylooh](https://discuss.kotlinlang.org/u/kylooh)\
**Post date:** [March 12, 2024, 10:49am UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309/2 "2024-03-12T10:49:15Z")

</div>

A new way to instance a anonymous object, I have the same question.

further more, when we need to return a functional interface, we write

```kotlin
fun something(): AnInterface {
    return AnInterface { }
}

```

instead of

```auto
return {}

```

but, we can use this conversion in the argument place

```auto
fun somethine(f: AnInterface) {

}

somethine({}) //or somethine {}

```

in this case, Kotlin translate `() -> Unit` to `AnInterface`

I thought Kotlin could translate function interface for return type too.

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [March 14, 2024, 5:46am UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309/3 "2024-03-14T05:46:40Z")

</div>

Well, `AnInterface { ... }` is syntactic sugar for the following:

```auto
object : AnInterface {
    override fun aFunction() {
        ...
    }
}

```

So if you have multiple functions, that syntactic sugar no longer works. I would assume that trying to use the same syntax as syntactic sugar for defining one function vs a block where you override multiple functions would introduce a lot of complexity into the compiler.

```auto
fun interface MyFunctionalInterface {
    fun myFunction(first: String, second: String): String
}

interface MyInterface {
    fun myFunction(first: String, second: String): String
    fun myOtherFunction(first: Int, second: Int): Int
}

val functional = MyFunctionalInterface { first, second ->
    first + second
}

val nonFunctional = MyInterface {
    override fun myFunction(first: String, second: String): String {
        return first + second
    }

    override fun myOtherFunction(first: Int, second: Int): Int {
        return first + second
    }
}

```

Although to us, the difference looks reasonably obvious, I can easily imagine it being quite complicated for the compiler.

---

<div class="post-metadata">

**Author:** ![phill](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/phill/32/8551_2.png) [@phill](https://discuss.kotlinlang.org/u/phill)\
**Post date:** [March 14, 2024, 9:00am UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309/4 "2024-03-14T09:00:02Z")

</div>

@Skater901 Yes you have captured exactly what I meant with a clear description, so many thanks for that.  
As to whether the compiler knows the difference, I am not an expert but it seems that the compiler knows enough to complain when you try the shortened form for a non-functional interface.

Phill

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [March 14, 2024, 8:48pm UTC](https://discuss.kotlinlang.org/t/enhanced-interface-implementation/28309/5 "2024-03-14T20:48:22Z")

</div>

Two points from my side:

1. Are there any other benefits of this than saving 8 chars? For functional interfaces we skip the function definition as well, so in the case of simple implementations like `it * 2`, actually 95% of the code is a bloat code that can be skipped. In the case of regular interfaces we can’t do that easily, so it is the other way around - by removing `object : ` we save maybe 5% of the code .

2. Goal of Kotlin is not to be as shortest as possible, but to be the most concise. `object : ` says explicitly we create an instance. Fun interface is a little different. It is something between an interface and a function type. If we create it with the mentioned syntax, we think of it as a function/lambda, but of a specific type. In some cases we can even skip its type name entirely. Such approach doesn’t make sense for non-functional interfaces where we always think in the terms of classes/objects/interfaces.
