In my opinion, any Option
implementation which exposes HasValue
property is the defeat of the entire idea. The point of optional objects is that you can make unconditional calls to their contents without testing whether the content is there.
If you have to test whether the optional object contains a value, then you have done nothing new compared to common null
tests.
Here is the article in which I am explaining optional objects in full detail: Custom Implementation of the Option/Maybe Type in C#
And here is the GitHub repository with code and examples: https://github.com/zoran-horvat/option
If you're reluctant to use a heavyweight Option solution, then you can easily build a lightweight one. You can make your own Option<T>
type which implements IEnumerable<T>
interface, so that you can leverage LINQ extension methods to turn calls optional. Here is the simplest possible implementation:
public class Option<T> : IEnumerable<T>
{
private readonly T[] data;
private Option(T[] data)
{
this.data = data;
}
public static Option<T> Create(T value)
{
return new Option<T>(new T[] { value });
}
public static Option<T> CreateEmpty()
{
return new Option<T>(new T[0]);
}
public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)this.data).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.data.GetEnumerator();
}
}
Using this Option<T>
type is done via LINQ:
Option<Car> optional = Option<Car>.Create(myCar);
string color = optional
.Select(car => car.Color.Name)
.DefaultIfEmpty("<no car>")
.Single(); // you can call First(), too
You can find more about optional objects in these articles:
And you may refer to my video courses for more details on how to simplify control flow using Option
type and other means: Making Your C# Code More Functional and
Tactical Design Patterns in .NET: Control Flow
The first video course (Making Your C# Code More Functional) brings detailed introduction to railway-oriented programming, including the Either
and Option
types and how they can be used to manage optional objects and handle exceptional cases and errors.
??
) which can be used in an evaluation to return a selected value if the object is null. – Duynenullable
DOES NOT fulfill this requirement.nullable
simply boxes primitive types into objects so that they can be nullable. This seems to be different from theOptional
class OP is describing – IridescenceOption<T>
makes the possibility of missing values explicit in the type. – JapanOption<T>
struct, since it's not standard, you've now added another way of representing a possibly missing value. You would need to convince everyone on your team to useOption<T>
and never return null references or useNullable<T>
. You'll also have to deal with converting to and from the existing representation to external code which uses them. – Japanundefined
of javascript. I wish they would all have a love-child. Nothing from T-SQL is allowed in. – Vocabularyref
orout
parameter instead? – Trifle