On 4 June 2012 15:25, clay <[email protected]> wrote: > The null coalescing operator aka the Elvis operator is a simple shorthand > for the traditional approach seen in C-derived languages: > > String something = something ? something : "default value"; // Ternary > operator in C, Java, JavaScript, C#, an many others > val something = something ?: "default value" // Groovy coalescing operator > (Elvis Operator) > String something = something ?? "default value"; // C# coalescing operator > > The advantage of Scala's Option is that it works properly with map and > fold type functions without any special null checking/coalescing syntax. >
and, most importantly, flatMap. This is the key to full composability and the one that simply can't be done with something like the elvis-operator solutions. > > Fantom and Kotlin (and Haskell) take this much further. By default, > references are non-nullable, and the compiler guarantees that they can > never contain null values at runtime. When nulls make sense, you can choose > to use a nullable reference. > > IMO, the best is the Fantom/Kotlin/Haskell route. Scala has the second > best option that is based on an intentional trade off with better Java > interop. A simple null coalescing operator is the weaker solution and that > is only slightly better than Java which requires slightly more syntax. > Haskell uses Maybe, which is directly equivalent to Scala's Option. It just does it a bit more elegantly because the lack of subtyping allows for full H-M type inference. > > On Sunday, June 3, 2012 9:43:48 PM UTC-5, Cédric Beust ♔ wrote: >> >> From a practical standpoint, I think that the "Elvis" approach which I >> first saw in Groovy and which is also available in Fantom and Kotlin is the >> best of both worlds. It doesn't offer the nice monadic properties that >> Option gives you, but it comes at a much cheaper price and less boiler >> plate. >> >> > -- You received this message because you are subscribed to the Google Groups "Java Posse" group. 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.
