Skip to content

Instantly share code, notes, and snippets.

@kmizu
Last active October 12, 2016 07:39
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 kmizu/81cd2a2940630fcab19feba1ba3e6a28 to your computer and use it in GitHub Desktop.
Save kmizu/81cd2a2940630fcab19feba1ba3e6a28 to your computer and use it in GitHub Desktop.
A question about relation between Kotlin's Unit and other types
fun foo(): Unit = run<Unit> {
1 + 1
}
fun add(x: Int, y: Int): Int = run {
x + y
}
fun main(args: Array<String>) {
/*
val x: Unit = add(1, 2)
error: type mismatch: inferred type is kotlin.Int but kotlin.Unit was expected
val x: Unit = add(1, 2)
^
*/
/*
It seems that implicit conversion from T to Unit is not defined.
Then, why does foo() function compiles ?
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment