Open cloudRoutine opened 7 years ago
I prefer using None instead of null in F#.
@radek, maybe semantics like
if a = null then None else Some (a.b) And chained together so
if a = null then None else (if b = null then None else Some (a.b.c)) Would allow easy matching as well: match a.?b with | Some b -> printf "a.b is %A" b | None -> printf "a is null"
There is already an Option.ofObj(F# 4) that can help here You can do something like, let a = new System.IO.FileInfo("Foo") let (<_>) a f = a |> Option.map f let ($) a f = (Option.ofObj a) <_> f let x = a$(fun x -> x.CreationTime) <*> (fun x -> x.ToShortDateString()) Not quiet as succinct but you can always lift those accessors to functions to make things tidier. In general though I'm opposed to using this type of symbolics as it hides meaning, however well documented it is from (C# etc...). I'd prefer to define these locally like this and just use the functions from F# core.
"maybe{}" is a basic lesson about computation expressions and it already solves problems, that "elvis operator" is trying to solve. F# doesn't need to solve monad problems using syntax sugar like C# does. We can use existing tools instead.
I agree with Bartosz. Even though ?. would be more succinct, I would prefer the built-in language symbols remain as few in number as possible.
this snippet is a superstrong argument against such a feature ;) http://pastebin.com/Z6kbuTEE (be proud if you can compute all types of the variables as well as the return value)
Introduce the ?. operator into F# [10837866]
Submitted by John Azariah on 11/23/2015 12:00:00 AM
[ 16 votes ]
Since we allow the . operator to reference fields and properties of objects in F#, we're faced with the same problem of null checking that plagued C# until C# 5. The C# 6 'elvis' operator propagates nulls in a succinct way, and I think that working with objects in F# will be similarly simplified if we introduce it here as well!