Skip to content

Instantly share code, notes, and snippets.

@pambrose
Last active May 30, 2018 03:48
Show Gist options
  • Select an option

  • Save pambrose/1a006db13ae299ff2ff059686d7c4917 to your computer and use it in GitHub Desktop.

Select an option

Save pambrose/1a006db13ae299ff2ff059686d7c4917 to your computer and use it in GitHub Desktop.
Nullability in Kotlin Generic Extension Function Receivers
fun main(args: Array<String>) {
val nonnNullStr = "Str-"
val nullStr: String? = null
println(nonnNullStr.nullableConcat("1"))
println(nullStr.nullableConcat("2"))
println(nonnNullStr.nonnullableConcat("3"))
// Why is this legal?
println(nullStr.nonnullableConcat("4"))
}
fun <T> T?.nullableConcat(b: T) = this.toString() + b.toString()
fun <T> T.nonnullableConcat(b: T) = this.toString() + b.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment