Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit operator for option type #9

Closed
janno-p opened this issue Sep 10, 2016 · 5 comments
Closed

Implicit operator for option type #9

janno-p opened this issue Sep 10, 2016 · 5 comments

Comments

@janno-p
Copy link

janno-p commented Sep 10, 2016

Would it be very awful if Option type had implicit operator for converting value from T => Option<T>?

public static implicit operator Option<T>(T value)
{
     return Option.Some(value);
}

It would allow to use shortcut for assigning option values. Instead of Option<bool> opt = Option.Some(true); I could use Option<bool> opt = true;

@nlkl
Copy link
Owner

nlkl commented Sep 10, 2016

Hi Janno,

I will consider this. In general I am not a big fan of implicit conversions, and it does leave some questions to what the conversion behavior should be, e.g. should it return value.Some() or maybe even value.SomeNotNull() etc.

Usually I simply use the extension methods, e.g. var opt = true.Some();, which leave very little syntactic overhead.

In my opinion, the only situation in which an implicit conversion "looks nicer", is when converting the return value (e.g. return true vs return true.Some()). However, this breaks the symmetry between .Some() and .None(), with very little benefit. As such, I do like explicitly choosing the conversion (.Some(), .None(), .SomeNotNull(), .SomeWhen(...), and so on).

Nevertheless, I will give it some thoughts. You are welcome to come up with more pros/cons.

/Nils

@janno-p
Copy link
Author

janno-p commented Sep 11, 2016

Ok. This is all reasonable and those extension methods for conversion really help a lot. Since there are many possible interpretations for this implicit behavior (some might prefer null conversion to None, others might expect Some(null)), then it's hard to make a preference. And when one choice is made it might introduce some unexpected behaviors for end users.

Considering that, I agree, that explicit conversion is much safer way to go and this question is resolved for me.

@janno-p janno-p closed this as completed Sep 11, 2016
@janno-p
Copy link
Author

janno-p commented Sep 11, 2016

Waiting for .NET Core compatible NuGet packages to be released 😄

@nlkl
Copy link
Owner

nlkl commented Oct 1, 2016

@janno-p Just FYI, .NET Core support has just been released. Enjoy :)

@janno-p
Copy link
Author

janno-p commented Oct 2, 2016

Great! I will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants