sksamuel
sksamuel

Reputation: 16387

Overloaded methods receiving higher order functions in Kotlin

Is it possible to have overloaded methods, each accepting a function and nothing else, eg

fun foo(a: () -> A) { }
fun foo(b: () -> B) { }

In Scala this is not possible, because the functions desugar to instances of Function0, and due to erasure these methods cannot be disambiguated. Is this the same case in Kotlin, and if so is there a workaround?

Upvotes: 1

Views: 569

Answers (1)

Andrey Breslav
Andrey Breslav

Reputation: 25777

You can work around signature clashes in Kotlin using the [platformName] annotation:

import kotlin.platform.*

class A
class B

[platformName("foo1")]
fun foo(a: () -> A) { }
fun foo(b: () -> B) { }

See the docs here

Upvotes: 1

Related Questions