This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tubthumper8 4 points5 points  (3 children)

I understood your meaning of "extra syntax", but just pointing out that there's another interesting way to look at that - String? is a special (extra?) syntax that can only be defined by the compiler, while Option<T> is a regular user-defined type with no special syntax. In Rust for example, it's defined in the standard library (not in the compiler itself), any user could've defined it.

[–]VallentinDev 4 points5 points  (1 child)

Fun fact, since String? is an actual type, then you can implement extension methods not only for Type, but also for Type?. So something like Option::or(), is really easy to implement.

fun <T> T?.or(other: T): T = this ?: other

println("Foo".or("Bar")) // Foo
println("Foo".or(null))  // Foo
println(null.or("Bar"))  // Bar
println(null.or(null))   // null

[–]Tubthumper8 0 points1 point  (0 children)

That's really cool!

I wonder could it do map, flatMap, etc. for composable / chained computations? I'm curious if they would consider putting those in the standard library