drharris•

Serious question: how is this better than Nullable? Seems like all that syntactic sugar (through extension methods) can be done over Nullables in the same way, without introducing a new type.

8
nlklOP•

A nullable can only wrap structs, rendering it practically useless for providing extra safety (as a struct cannot be null anyway).

Further, a nullable does not expose a very safe API. The Value property can easily blow up, if you don't check for existence first. But you are not forced to check for existence.

In that way, nullables might look similar on the surface, but they are conceptually quite different underneath.

That being said, an optional can be used for more or less anything that a nullable can be used for, just in a more safe manner.

2
drharris•

Ok, I see. The examples seemed to primarily be value types, so it was difficult to see the benefit.

Right now it seems clunky, but with C#6 support for using static members, this could be big (i.e. being able to do val n = None(10);).

I find myself moving toward a more functional C# style, so this could be interesting.

1
nlklOP•

For a more full-fledged functional approach, using C# 6 features, take a look at the interesting language-ext library: https://github.com/louthy/language-ext

It offers a quite similar option type. Mine is a focused (and simple) alternative to all of us that use C# here and now, or people who don't want the extra functionality :)

1
AngularBeginner•

A nullable can only wrap structs, rendering it practically useless for providing extra safety (as a struct cannot be null anyway).

Correction: It can only wrap value types, not only structs.

1
nlklOP•

Well, isn't the distinction a bit vague? Certainly, it can wrap primitive types, but as far as I know these are considered "structs" as well - even though it isn't possible to re-implement them as structs yourself.

[SerializableAttribute] public struct Nullable<T> where T : struct

1
AngularBeginner•

it can wrap primitive types, but as far as I know these are considered "structs" as well

A struct is a value type, but a struct is not a primitive.

1
Banane9•

All structs are value types and all value types are structs.. And giving the code snippet, struct would be correct anyways. Because that's literally what the code says.

And primitives are structs too, they just have built-in literals.

1
recursive•

enums aren't structs in the sense that people usually ever talk about structs.

1

I prefer to use a extension method which I've posted before:

public static class Extensions {     public static TResult IfNotNull<T, TResult>(this T source, Func<T, TResult> predicate, TResult defVal = default(TResult)) where T : class     {         if (source != null) { return predicate(source); }         return defVal;     } }

1
grauenwolf•

We all ready have an option type for C#, it's called System.Object.

What we need is a non-optional type.

3
emn13•

Cool project!

A few observations:

  • You might want to consider what it means to create an Option.Some(default(string)) - option, as is, doesn't actually prevent nulls, it just makes intentional nulls more obvious.

  • You can use an implicit conversion operator with a special OptionNone type to avoid the dirty Option.None(meaninglessValueHere) parameter.

If you're interested in a collaborator, I'm in :-).

1