Skip to content

Instantly share code, notes, and snippets.

@tanmatra
Created October 3, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanmatra/3175320b60103e8d22808b629b4ebef5 to your computer and use it in GitHub Desktop.
Save tanmatra/3175320b60103e8d22808b629b4ebef5 to your computer and use it in GitHub Desktop.
Kotlin SAM conversions
import java.util.Date;
import java.util.function.Consumer;
public interface Caller
{
void call(String id, Consumer<Integer> integerConsumer, Consumer<String> stringConsumer, Consumer<Date> dateConsumer);
}
fun testCaller3(caller: Caller) {
val consumer1: Consumer<Int> = Consumer { int: Int -> println(int) }
val consumer2: Consumer<String> = Consumer { string: String -> println(string) }
val consumer3: Consumer<Date> = Consumer { date: Date -> println(date) }
val function1 = { int: Int -> println(int) }
val function2 = { string: String -> println(string) }
val function3 = { date: Date -> println(date) }
caller.call("id", consumer1, consumer2, consumer3) // OK
caller.call("id", function1, function2, function3) // OK
caller.call("id", consumer1, function2, function3) // Error
caller.call("id", consumer1, consumer2, function3) // Error
caller.call("id", function1, consumer2, consumer3) // Error
caller.call("id", function1, function2, consumer3) // Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment