Giter Site home page Giter Site logo

Comments (6)

vkhorikov avatar vkhorikov commented on September 13, 2024 1

Published 1.11.0

from csharpfunctionalextensions.

FrancoisM avatar FrancoisM commented on September 13, 2024

It's because of

public bool HasValue => _value != null;

If it only works for T being a class then you should add where T: class

from csharpfunctionalextensions.

FrancoisM avatar FrancoisM commented on September 13, 2024

Or better: replace with

public bool HasValue => _value != default;

from csharpfunctionalextensions.

freakingawesome avatar freakingawesome commented on September 13, 2024

Or better: replace with

public bool HasValue => _value != default;

Comparing against default would have the side-effect of making zero a None value, which is probably unintentional.

I have submitted a pull request which wraps the value inside Maybe<T> in a class so that null checks work with structs as well as classes.

from csharpfunctionalextensions.

FrancoisM avatar FrancoisM commented on September 13, 2024

Indeed in my case 0 is a None value but it may not always be the case.
I'm not sure about the wrapper.
The idea of Maybe is to give a type to null. As a struct cannot be null, then I think Maybe should not be used with a struct in the first place. My mistake. I'm more in favor of where T: class now that I think about it.

from csharpfunctionalextensions.

freakingawesome avatar freakingawesome commented on September 13, 2024

I disagree that Maybe<T> is only about replacing null. It also gives you functor composability that otherwise wouldn't exist by using .Select(...). Here's a contrived example:

Maybe<string>.None.Select(_ => 42)

This would be impossible if we add the class type constraint, and currently this fails because the None maps to a Maybe<int> that has a value of zero, when logically it should map to None.

I updated my PR to fix these mapping issues as well.

from csharpfunctionalextensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.