HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Option type in C# and implicit conversions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implicitconversionstypeoptionand

Problem

I have decided to create a simple Option type for C#. I have essentially based it on the
Scala's Option.

It was mostly a fun exercise, but it might come in handy at work or any other project, even though it's less powerful than the Scala version.

Now I'd appreciate all remarks regarding this little piece of code, but essentially now I'm thinking whether or not to introduce an implicit conversion between the type and the Option, which would allow for:

MyType obj;
// ... something interesting happens (or not)
Option optionalObj = obj;


I have already included ToOption extension method for convenient creation of the option from existing object:

var optionalObj = obj.ToOption();


which returns either Some or Noneand the implicit conversion would do just that, but I'm just wondering if it's not too 'hidden', non-obvious and/or counter-intuitive(?).

Anyway, I'd like to see what you think.

```
public abstract class Option
{
///
/// Option's value
///
public abstract T Value { get; }

///
/// Indicates whether the option holds a value or not
///
public abstract bool IsEmpty { get; }

///
/// Unwraps the option.
///
///
/// Specified, default value if the option is empty, or the option's value if present
/// It's recommended to use the when passing a parameter expression to be evaluated.
/// In this case, the condition will get evaluated AFTER evaluation of the parameter, which may be costly.
public T GetOrElse(T defaultValue)
{
return IsEmpty ? defaultValue : Value;
}

///
/// Unwraps the option
///
/// Function returning the default value
/// The evaluation of the specified function if option is empty, or the option's value if present
/// Recommended overload. The function returning defaultValue will only get evaluated if
/// the option is empty.
public T GetOrElse(Func defaultValue)
{
return IsEmpty ? defau

Solution

-
You have several extension methods with Option as first parameter, why don't you use normal instance methods?

-
I prefer having an implicit conversion from T to Option

Option seems like a nice choice for optional function parameters. In that case calling ToOption() everywhere is a bit annoying.

-
Why make the initialization of None lazy? Creating a None is probably cheaper than creating a Lazy. I recommend lazy initialization only if it's expensive.

-
I'd rename IsEmpty to HasValue to match Nullable. (Inverted meaning obv.)

-
I'd add some kind of protection against other classes deriving from Option. For example an internal abstract method.

-
public abstract override string ToString(); Seems weird. Don't touch it on Option and simply override it in the child classes.

-
Why disallow a value of null?

-
I prefer using a struct, so the option itself cannot be null.

-
One could consider implementing IEnumerable, instead of a ToEnumerable() method. But I'm not sure if that's a good idea.

Context

StackExchange Code Review Q#21461, answer score: 4

Revisions (0)

No revisions yet.