tejacques / option Goto Github PK
View Code? Open in Web Editor NEWAn Option type for C#
License: MIT License
An Option type for C#
License: MIT License
I am aware of the benefits of having the 'Option'-type implement IEnumerable. However, is there a specific reason why the 'ForEach'-method is titled as such? In a codebase I'm currently working in it unfortunately conflicts with a 'roll-your-own'-implementation of a LINQ-style 'ForEach'-extension method.
My opinion is obviously subjective and biased, but I was wondering why the name 'ForEach' was settled. I could not find 'ForEach' being used in F# either. 'Apply' feels like the better choice.
I love this library though; it's awesome!
Currently, there are 7 methods to use the value in an option:
TryGetValue
if(option.TryGetValue(out value))
{
/* use value */
}
HasValue
and Value
if(option.HasValue)
{
var val = option.Value;
}
ValueOrDefault
and ValueOr
var value = option.ValueOrDefault;
value = option.ValueOr(1);
value = option.ValueOr(() => 1)
Match
method using Some
and None
argumentsoption.Match(None: () => {}, Some: value => {});
IEnumerable
interfaceoption.Select(value => value);
Match
method using Some
and None
fluent APIoption.Match()
.None(() => {})
.Some(value => {})
.Result();
Match
method using overloaded |
and implicit conversionsvar matched = option.Match()
| value => value
| () => 0;
I believe that there is value to having multiple ways to access the data, because it allows you to pick the one most natural for the situation. That said, some of these methods are clearly inferior and can lead to a lot of the issues the Option type is supposed to help prevent.
The option type should help prevent these things:
Access to the Option's value through option.Value
does not prevent runtime exceptions, thus it should be removed.
The functional style OptionPatternMatcher
also doesn't prevent runtime errors or non-exhaustive matching, in addition to having a syntax which looks like another language (more like F# or ML, which isn't bad, but is confusing to users of the library in C#). If there was compiler support for it, I would leave it in, but because there isn't, and it does still have runtime errors, they should be removed.
Therefore we should remove the 2nd, 6th and 7th methods for accessing the value of the option. This will consolidate the methods down to 4.
To help migrate projects using these styles, we should release a 3.1.0 version which marks these as obsolete with instructions on how to restructure the code. Once this version is released, in the wild for an appropriate amount of time, a 4.0.0 library should be released which removes these.
Since your implementation doesn't use class hierarchy (like MayBe from FunctionalCSharp does), it should be struct (like Nullable). It will at least protect from NullReferenceExceptions which is the goal of maybe/option types in Scala and F#.
This should be removed because it isn't a safe operation. Instead use one of the following if a value is required:
Option.ValueOrDefault
Option.ValueOr(T value)
Option.ValueOr(Func<T> value)
Option.Match(Func<TResult> None, Func<T, TResult> Some)
It should be easy to compile source code against .netstandard1.1 target, since it only references BCL.
I have mixed feeling about doing this, but it's neat. In order to use it, you need to wrap the lambdas in parens, because otherwise C# doesn't know what to bitwise OR |
together
let o = Some(1)
let m = match o with
| None -> "None"
| Some(i) -> string i
Option<int> o = 1;
var matcher = o.Match()
| ((i) => { /* Do something */ })
| (() => { /* Do something else */ });
Func<string> m = () => o.Match<string>()
| ((i) => i.ToString())
| (() => "None");
This isn't best practice in case Microsoft implements an official version in the future.
If we do this, we can take advantage of LINQ on the Option type.
Idea from: https://github.com/louthy/language-ext
This allows for runtime checked Non-null types.
Please, update Option NuGet package, the last published version is 2.0.2, but I want to use "null-safe" version. Thanks
I am currently stuck on .NET 4.0. Can you provide a binary for it on NuGet?
This makes them more Scala-like.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.