Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. using LanguageExt;
  2. using static LanguageExt.Prelude;
  3.  
  4. ///////////////////////////////////////////////////////////////////////////////////
  5. // Option<T> doesn't allow null use at all, and it's a struct, so any references
  6. // also can't be null.
  7.  
  8. string noValue = null;
  9. Option<string> str1 = noValue; // Implicitly coerced to None
  10. Option<string> str2 = Optional(noValue); // Explicitly coerced to None
  11. Option<string> str3 = None; // None
  12. Option<string> str4 = Some(noValue); // ValueIsNullException is thrown
  13. Option<string> str5 = Some("Hello"); // Some of "Hello"
  14. Option<string> str6 = Optional("Hello"); // Some of "Hello"
  15.  
  16. ///////////////////////////////////////////////////////////////////////////////////
  17. // Matching return values are also checked for null
  18. var str = Some("Hello, World");
  19. var res = match(str,
  20. Some: x => noValue,
  21. None: () => noValue); // ResultIsNullException is thrown
  22.  
  23. ///////////////////////////////////////////////////////////////////////////////////
  24. // For the rare occasions where you want an optional value that can represent
  25. // null, there is OptionUnsafe<T>.
  26.  
  27. OptionUnsafe<string> unsstr1 = None; // None
  28. OptionUnsafe<string> unsstr2 = SomeUnsafe(noValue); // Some(null)
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. // LanguageExt Option<T> doesn't allow access to a Value property, the only way of
  32. // getting at the value is via Match, Map, Bind, Iter, Fold, Sum, Filter, Exists,
  33. // ForAll, ... all of which take the state of the discriminated union into account.
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////////
  36. // FSharpOption<T> allows access to the underlying .Value property, which makes it
  37. // fundamentally unsafe, and pretty much pointless (for use in C#). You may as well
  38. // use a reference for all the safety it gives you.
  39.  
  40. var someValue = FSharpOption<string>.Some("Hello");
  41. var noneValue = FSharpOption<string>.None; // noneValue == null
  42.  
  43. Console.WriteLine(someValue.Value);
  44. Console.WriteLine(noneValue.Value); // This will throw a NullReferenceException
  45.  
  46. ///////////////////////////////////////////////////////////////////////////////////
  47. // language-ext provides conversion functions for F# types to make
  48. // interop simple:
  49.  
  50. var someValue = FSharpOption<string>.Some("Hello");
  51.  
  52. // Convert from FSharpOption<T> to Option<T>
  53. Option<string> opt = fs(someValue);
  54.  
  55. // Convert back to FSharpOption<T> from Option<T>
  56. FSharpOption<string> fsopt = fs(opt);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement