concept none in category c#

appears as: None, Nones
Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

Listing 3.6. Idealized relation of Option to its cases None and Some
namespace LaYumba.Functional { interface Option<out T> { } namespace Option { public struct None : Option<T> { /* ... */ } public struct Some<T> : Option<T> { /* ... */ } } }

That is, I’d like to say that None is an Option<T>, and so is Some<T>. Unfortunately, there are several problems with the preceding code (which, as a consequence, doesn’t compile):

  • None doesn’t have (and doesn’t need) a type parameter T; it can’t therefore implement the generic interface Option<T>. It would be nice if None could be treated as an Option<T> regardless of what type the type parameter T is eventually assigned, but this isn’t supported by C#’s type system.
  • An Option<T> can only be one of two things: None or Some<T>. It shouldn’t be possible for any client assembly to define any other implementations of Option<T>, but there’s no language feature to enforce this.
  • Given these issues, using an interface or abstract class for Option doesn’t work very well. Instead, I defined Option<T> as a separate class and defined methods so that both None and Some<T> can be implicitly converted into Option<T> (inheritance by implicit conversion, if you like).

    Listing 3.7. Option<T> can capture both the Some and None states

    This implementation of Option can represent both None and Some; it has a Boolean value to discriminate between these two states, as well as a field of type T to store the inner value of a Some.

    You can now treat None as an Option<T> for any type T. When None is converted to an Option<T>, the isSome flag will be false; the inner value will be the default value for T and will be disregarded. When Some<T> is converted into an Option<T>, the isSome flag is true and the inner value is stored.

    I also added a method to implicitly lift a value of type T into an Option<T>, which will prove convenient in some scenarios. It yields an Option in the None state if the value is null, and it wraps the value into a Some otherwise.

    Partial functions are problematic because it’s not clear what the function should do when given an input for which it can’t compute a result? The Option type offers a perfect solution to model such cases: if the function is defined for the given input, it returns a Some wrapping the result; otherwise, it returns None.

    Lookup takes a NameValueCollection and a string (the key), and will return Some with the value if the key exists, and None otherwise. Here’s the implementation:

    public static Option<string> Lookup
       (this NameValueCollection @this, string key)
       => @this[key];

    That’s it! The expression @this[key] is of type string, whereas the return value is Option<string>, so the string value will be implicitly converted into an Option<string>. (Remember, in the implementation of Option shown earlier, implicit conversion from a value T to an Option<T> was defined to return None if the value was null, and to lift the value into a Some otherwise.) We’ve gone from a null-based API to an Option-based API.

    If you need to create an Age from an int, instead of calling the constructor (which has to throw an exception if it’s unable to create a valid instance), you can define a function that returns Some or None to indicate the successful creation of an Age. This is known as a smart constructor: it’s “smart” in the sense that it’s aware of some rules and can prevent the construction of an invalid object.

    Listing 3.8. Implementing a smart constructor for Age

    If you now need to obtain an Age from an int, you’ll get an Option<Age> instead, which forces you to account for the failure case. If your Option<Age> is None, what do you do with it? Well, that depends on the context and requirements. In upcoming chapters we’ll look at how you can work effectively with Options. Although Match is the basic way of interacting with an Option, we’ll build a rich, high-level API starting in the next chapter.