Actually, this flatMap snippet shows exactly why it's a horrible fit for 
java. This introduces the concept of flatMap which is a gigantic can of 
worms. Yes, people who are used to functional languages find this as 
obvious and effortless to read, write, and understand as a java programmer 
who sees 'while'. It's an entirely different way of thinking, a _HUGE_ 
mental burden.

You are not Joe Java. Joe Java does not read this and instagrok what's 
going on here. This is a problem. A show-stopper, even.

Any language proposal to hoist nullity into the type system surely should 
also come with various useful riders attached, such as elvis operators and 
the like. Then the code snippet becomes something Joe Java would, in fact, 
grok just fine:

A? varAMightBeNull = methodThatMayProduceA();
B? varBMightBeNull = varAMightBeNull.?getB();


note that your code snippet involves a static method call called 
'tryToGetBFromA', which can _OF COURSE_ check if the incoming param is null 
and if so, return null. So, your actual snippet doesn't even _NEED_ option. 
I'm not sure if you did this because you are just not aware of what 
idiomatic java looks like, or if you had to write extremely convoluted java 
code in order for your flatMap example to work. I don't know the specific 
of flatMap, but I'm guessing that, if tryToGetBFromA was a no-args instance 
method, your flatMap wouldn't work as written.


On Friday, July 27, 2012 2:30:17 AM UTC+2, clay wrote:
>
> Here is a simple example that illustrates the benefit of Option beyond 
> avoiding null exceptions:
>
> Here is a traditional piece of logic using traditional null if-checks. 
> This is using Scala syntax but would be equivalent in Java.
>
> val variableAMayBeNull: A = methodThatMayProduceA();
> if (variableAMayBeNull != null) {
> val variableBMayBeNull: B = tryToGetBFromA(variableAMayBeNull);
>
> if (variableBMayBeNull != null) {
> doSomethingWithNonNullB(variableBMayBeNull);
> }
> }
>
> Same logic using Option. This is Scala syntax. Java can do this but not as 
> nicely until it gets lambdas.
>
> val variableAMayBeNull: Option[A] = methodThatMayProduceA();
> val variableBMayBeNull: Option[B] = 
> variableAMayBeNull.flatMap(tryToGetBFromA);
> variableBMayBeNull.foreach(doSomethingWithNonNullB);
>
> Both blocks are logically equivalent, but the Option route is much more 
> concise, elegant, and maintainable. The if-logic is moved from the end 
> application code to inside of the Option class. Since this type of logic is 
> so common in application code, the code simplification benefits are quite 
> large.
>
>  

-- 
You received this message because you are subscribed to the Google Groups "Java 
Posse" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/javaposse/-/L_ZlenOaaM4J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to