question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maybe.Some(null) does not throw and has a different behavior than Maybe.None()

See original GitHub issue

I’m currently testing SuccincT, quite happily so far, but encountered a problem when trying to integrate it in a part of a project I’m working on. Why is Maybe<string>.Some(null) allowed ?

var someNull = Maybe<string>.Some(null);

in this case, someValue.HasValue returns true and someNull.Value returns null, where I would have expected this to behave the way Maybe<string>.None() does (after all, it’s seems to be the same thing semantically speaking), or at least to throw.

Of course this is not senseless, but in my mind it defeats the objective of Maybe, which is to provide a way of avoiding nulls. That means that if a legacy part of the project passes an unchecked value which turns to be null, Maybe will happily live with it, propagating it without complaining, meaning that I still need to rely on defensive code even in a Maybe enabled codebase.

Is there a reason I don’t get behind this behavior ?

If not, which behavior would you recommend / prefer ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidArnocommented, Feb 17, 2020

The following was implemented in v4:

Assigning null to Some will throw, An implicit cast from null to an option will result in a none, Option<RefType>.None == null.

Closing as implemented.

1reaction
DavidArnocommented, Jun 21, 2018

Well it turns out that the changes I’m making for v4 effectively force my hand here. I’ve never been happy with having Option<T> and Maybe<T>, so the latter is going and the former is becoming a struct. To make that play nicely with null:

  1. Assigning null to Some will throw,
  2. An implicit cast from null to an option will result in a none,
  3. Option<RefType>.None == null.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Maybe/Option type in TypeScript using class ...
I have tried using never , null and any and they all lead to (different) compiler errors that force me to do some...
Read more >
c# - Is it unreasonable to expect Any() *not* to throw a null ...
Yes, simply because you're in C# and that behavior is well defined and documented. If you were making your own library, or if...
Read more >
What happens if you throw `null`? - The C# Player's Guide
It creates an error that can't be caught, because no handler can catch null . Nothing happens because no exception is provided to...
Read more >
The Maybe Monad (C#)
In C#, null is a valid value for variables whose types are reference types (e.g. ... means that no other classes can inherit...
Read more >
Understanding null safety
Code should be safe by default. If you write new Dart code and don't use any explicitly unsafe features, it never throws a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found