# Kotlin SAM interfaces

**URL:** <https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249>\
**Category:** Uncategorized\
**Created:** [March 1, 2024, 7:30am UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249 "2024-03-01T07:30:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![webber.steven](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/webber.steven/32/10318_2.png) [@webber.steven](https://discuss.kotlinlang.org/u/webber.steven)\
**Post date:** [March 1, 2024, 7:30am UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249/1 "2024-03-01T07:30:42Z")

</div>

Hi.

I ran into a problem recently while upgrading dependent libraries where a new function was added that overloaded one I was using. The compiler started throwing a compile time error.

Given this function

```auto
fun <T> executeWithNull(action: (ofy: SomeObject) -> T): T? {
    val callable = Callable {
        return@Callable SomeObjectService.run {
            val value = SomeObjectService.value()
            action(value)
        }
    }

    return this.timer.time(callable)
}

```

Where the SomeObjectService has a function **run** , as below, all works. I’m assuming Kotlin is building an object with a single method in order to map the lambda to the method parameter below.

```auto
	public static <R> R run(final Work<R> work) {
		try (Closeable closeable = begin()) {
			return work.run();
		}
	}

```

Now I add another run method to the SomeObjectService and Kotlin throws a compile time error.

So adding

```auto
public static void run(final Runnable work) {
	factory().run(work);
}

```

**inferred type is Unit! but T? was expected**

I was hoping to understand if this is a Kotlin compiler “bug” or if I’m just expecting to much (Mapping to the correct method based the return type of the lambda supplied) or if I’m just not understanding the issue at all.

PS wrapping the lambda passed to the function in a new object (Work) works.

---

<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 1, 2024, 8:50am UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249/2 "2024-03-01T08:50:00Z")

</div>

I think this: “I’m just expecting to much (Mapping to the correct method based the return type of the lambda supplied)”. it doesn’t sound trivial to do.

I’m not exactly sure why it chose the second `run` method. Maybe it considered the first to be more generic.

---

<div class="post-metadata">

**Author:** ![frozenice](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/frozenice/32/7436_2.png) [@frozenice](https://discuss.kotlinlang.org/u/frozenice)\
**Post date:** [March 7, 2024, 3:26pm UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249/3 "2024-03-07T15:26:32Z")

</div>

> [@broot](#):
>
> Maybe it considered the first to be more generic.

Indeed, when there is overload ambiguity:

> Any non-parameterized callable is a more specific candidate than any parameterized callable

(more details in [this thread](https://discuss.kotlinlang.org/t/no-error-for-ambiguous-method-call/27638))

---

<div class="post-metadata">

**Author:** ![webber.steven](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/webber.steven/32/10318_2.png) [@webber.steven](https://discuss.kotlinlang.org/u/webber.steven)\
**Post date:** [March 7, 2024, 3:55pm UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249/4 "2024-03-07T15:55:14Z")

</div>

Thanks for responses.

The workaround (implement the interface), was simple enough once I understood the issue.

My bigger concern though is that a 3rd partly library change broke my code even though the library didn’t change anything about the API I was using. Seems I traded off maintainability and consistency for ease of use/less typing. ☹

---

<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 7, 2024, 9:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-sam-interfaces/28249/5 "2024-03-07T21:21:36Z")

</div>

> [@webber.steven](#):
>
> My bigger concern though is that a 3rd partly library change broke my code even though the library didn’t change anything about the API I was using.

I think that shouldn’t concern you. Adding a new method is also an API change. It would be pretty scary if it would change the behavior without any notice. But if it fails to compile, then I think this is fine.
