

Exploring the C# 6 Null Propagation Operator - pquip
http://www.volatileread.com/Wiki?id=2104

======
acjohnson55
While I have some appreciation for the fact that Scala doesn't make its
`Option` type special from a syntactic standpoint--it's a sum type, like any
other--I think there's something to be said for the type union with syntactic
support. `optionalData?.methodOption?.method` is much more succinct than
Scala's `optionalData.flatMap(_.methodOption).map(_.method)`, or `for (data
<\- optionalData; result <\- data.methodOption) yield result.method`. Scalaz
seems to provide some more concise aliases for the monadic chain, but there's
still nothing I know of that gives you exactly the method access within a
monad quite like `?.`.

~~~
justthistime_
Scala encourages people to not use nulls everywhere, so adding syntactic sugar
for a special case, which is not even idiomatic Scala doesn't sound like a
good idea.

Option is not a "replacement" for null. Sometimes another type is more
appropriate to replace a usage of a null in Java/C#, so making Option special
would be counter-productive.

~~~
acjohnson55
I wouldn't use null anywhere in my Scala code, unless I had to in order to
interface with a Java library. I'm talking about sugar for working with
Option.

~~~
justthistime_
Where would this sugar stop? At Either? At Try? At Validation?

The problem is that if you introduce the sugar only for Option, users will
tend to use Option even in cases where other type constructors would be more
appropriate.

------
EvenThisAcronym
The null coalescing operator is nice, but it can be overused. I recently saw
some Swift code that was riddled with question marks everywhere,
which()?.made?.the?.code?()?.very?.difficult?.to?().read.

~~~
WorldWideWayne
I wonder if it would be useful to have some kind of block scope for null
coalescing? (Alternatively, why can't the programmer just specify the root
coalesce and have the compiler figure out the rest?)

Beyond that, I imagine the solution is to just bake null coalescing into the
language somehow. Then maybe you'd want to have a block scope for non-null
coalescing code.

------
ahuth
Was CoffeeScript the first to do this? Not aware of any other languages that
do.

This is one of the things I really miss when going back to JS.

~~~
Arguggi
This seems pretty similar to the Haskell Maybe Monad.

"Learn You a Haskell for Great Good!" has an example[0]:

    
    
      routine =   
        case Just (0,0) of   
            Nothing -> Nothing  
            Just start -> case landLeft 2 start of  
                Nothing -> Nothing  
                Just first -> case landRight 2 first of  
                    Nothing -> Nothing  
                    Just second -> landLeft 1 second
    

becomes (using some 'do' syntactic sugar):

    
    
      routine = do  
        start <- return (0,0)  
        first <- landLeft 2  
        second <- landRight 2 first  
        landLeft 1 second
    

Checking for Nothing is taken care of 'automatically' and if any of start,
first, second or (landLeft 1 second) are equal to Nothing routine is equal to
Nothing too.

[0] [http://learnyouahaskell.com/a-fistful-of-monads#walk-the-
lin...](http://learnyouahaskell.com/a-fistful-of-monads#walk-the-line)

~~~
acjohnson55
It's basically the same thing, except with that syntax, you have to label all
the intermediate results

~~~
bbcbasic
To avoid labeling:

    
    
        routine :: Int -> Maybe Int  
        routine n = Just n >>= (divisorFilter 3) >>= (divisorFilter 2) >>= (divisorFilter 5)
    
        -- Supporting function
        divisorFilter :: Int -> Int -> Maybe Int
        divisorFilter d n
            | n `mod` d == 0 = Just n
            | otherwise = Nothing
            
        -- Using do syntax
        routine' :: Int -> Maybe Int  
        routine' n = do  
            first <- divisorFilter 3 n
            second <- divisorFilter 2 first
            divisorFilter 5 second

------
benologist
This looks pretty useful, is there an equivalent in typescript?

------
rdsnsca
Apple's Swift has it too

------
tbrownaw
Yay monads?

