gojuno / koptional

Minimalistic Optional type for Kotlin that tries to fit its null-safe type system as smooth as possible.
Apache License 2.0
289 stars 21 forks source link

Introduce infix `or` function #10

Closed dmitry-novikov closed 7 years ago

dmitry-novikov commented 7 years ago

It will be really handy to have infix or function in Optional for fallback to default value in case of None like:

val some = Some(1)
val someOrDefault = some or 2
artem-zinnatullin commented 7 years ago

What I don't like about it is that it adds a second way to get default value to existing one o.toNullable() ?: "fallback", that was one of the things we were trying to remove — ambiguity…

Also in a real-life case, it might be not obvious because or is also defined for Int, Long and Boolean by Kotlin stdlib which could be used as Optional<> values quite often and might lead to wrong results of operation.

However, it's a shorter notation and fits Optional use case quite well…

Any input @ming13, @igor-korotenko, @nostra13, @igorab?

nostra13 commented 7 years ago

I agree with @artem-zinnatullin about possible confusing or considering existing binary operator. some or 2 and some.value or 2 are similar but will produce different results.

arturdryomov commented 7 years ago

Against it for two reasons.

artem-zinnatullin commented 7 years ago

Closing this for now then