5 ms·
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.
by ahuth 12y ago
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.
- biehl 12y agoGroovy has it too. http://mrhaki.blogspot.dk/2009/08/groovy-goodness-safe-navigation-to.html http://mrhaki.blogspot.dk/2009/08/groovy-goodness-safe-navig...
- vorg 12y ago> http://mrhaki.blogspot.dk/2009/08/groovy-goodness-safe-navigation-to.html http://mrhaki.blogspot.dk/2009/08/groovy-goodness-safe-navig... August 2009 is a long time ago, when Groovy 1.6 was the current version. The month before, Groovy's creator published a blog saying if he'd known about Scala he would never have created Groovy. And during the following month, Alex Tkachman published his first blog outlining his plans for Groovy++, which was eventually duplicated as the static typing facility in Groovy 2.x. > println company?.address?.street?.name I haven't used the Codehaus/Apache edition of Groovy since version 1.8 when there had been plenty of mailing list discussions about enabling ?. functionality to ripple through all subsequent . after the first use of ?. so that: println company?.address.street.name would be behave the same. Was this much talked about functionality ever implemented? Edit: Looks like the Haskell Maybe monad described by Arguggi in the other thread goes even one better than that by not even requiring their equivalent of ?. in the path at all but instead the typing system infers whether to use . or ?. functionality from the type of each field (company, address, and street in the example). I remember a mailing list discussion in the early days of Groovy begun by its creator James Strachan about whether to use some new syntax to require an object to be assigned to a variable: Object! ob= new Object() //can't be null Object ob //can be null Unfortunately, he was, er, replaced by the current management soon afterwards.
- ld00d 12y agoSwift has it too.
- M8 12y agoWhy wouldn't. It was created very recently.
- Arguggi 12y agoThis 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-line http://learnyouahaskell.com/a-fistful-of-monads#walk-the-lin...
- acjohnson55 12y agoIt's basically the same thing, except with that syntax, you have to label all the intermediate results
- bbcbasic 12y agoTo 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
- bbcbasic 12y agoWhich goes to show that what requires a 'new feature' in C# is just a freebie in Haskell, due to the powerful type system. See my other comment ITT for how to do this in a tighter way that is more like the C# syntax. (You just use the bind operator rather than wrapping it in a do)
- kristianp 11y agoThis isn't really the same thing. Haskell (and Ocaml) make you deal with the null case, whereas C# just lets you propagate the nulls. Haskell's way is safer, c# is just handy syntax.