#1168 How to check nullable bool?

Akcelisto Wed 4 Aug 2010

// What option do you prefer?
Future? future
// option A
if(future?.isDone==true){...}
// option B
if(future!=null && future.isDone){...}
// option C
if(future?.isDone?:false){...}

ivan Wed 4 Aug 2010

I always use option C in such cases

JohnDG Wed 4 Aug 2010

Don't use null. Use Option or Maybe (don't think something like this exists yet in Fantom, would be easy to add though).

cbeust Wed 4 Aug 2010

Why would we want Option/Maybe when we have the safe invoke (?.) operator?

DanielFath Wed 4 Aug 2010

I'd go for A.

Funny I was going to link to Cedric's note on Option/Maybe.

helium Wed 4 Aug 2010

@JohnDG: Fantom doesn't have generics. Do you really want a non generic Option/Maybe?

JohnDG Wed 4 Aug 2010

Option doesn't require a developer to remember to use safe invoke. It's more foolproof. It also forces a more functional style of programming, because the object is to convert an option of something into an option of the thing you want -- not to maybe perform some side effect if a chain of objects leading to the method are all non-null.

@JohnDG: Fantom doesn't have generics. Do you really want a non generic Option/Maybe?

I'm not sure. Obviously there is a lot more potential for type errors, but the code won't be substantially more verbose due to auto-casting (I imagine the occasional type hint will be needed, especially during map/flatmap).

What do you do inside the conditional if the future is both non-null and done?

If future was an Option, then the above would become something like:

r = future.filter(_.isDone).map(???) (Scala)

r = future.filter(function(f) { return f.isDone(); }).map(???) (EcmaScript derivatives)

r = future.filter{it.isDone}.map{...} (Fantom)

Login or Signup to reply.