Git Product home page Git Product logo

nullpropagationvisitor's Introduction

Nuget Fuget GitHub

NullPropagationVisitor

Expression tree is an amazing feature of C#, however it only supports a limited subset of C# features.
While there is no native support to null propagation operator a?.b, this library can do the job as an expression visitor.
Just call Visit method of the visitor and you are done!

As well the ?. operator, the visitor evaluates its left-hand operand no more than once.
You can use projects like ReadableExpressions to check the expression returned by visitor.

Examples of use

Check some examples of use in unit tests

Transformation

It works from the basic cenarios:

Expression<Func<string, int>> ex = str => str.Length;

which become something like

(string str) =>
{
    string caller = str;

    return (caller == null) ? null : (int?)caller.Length;
}

To the complex ones

Expression<Func<string, char>> ex = str => str == "foo" ? 'X' : Self(str).Length.ToString()[0];

which become something like

(string str) => (str == "foo")
    ? (char?)'X'
    : {
        string caller =
        {
            int? caller =
            {
                string caller = Program.Self(str);

                return (caller == null) ? null : (int?)caller.Length;
            };

            return (caller == null) ? null : ((int)caller).ToString();
        };

        return (caller == null) ? null : (char?)caller[0];
    }

nullpropagationvisitor's People

Contributors

leandromoh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nullpropagationvisitor's Issues

The Visitor changes expression type

Hi,

following test will not pass:

#nullable enable

record Foo(string? Name);

Expression<Func<Foo, object?>> propertyLambda = foo => foo.Name;

var visitor = new NullPropagationVisitor(true);
var result = visitor.Visit(propertyLambda); // returns Expression<Func<Foo, string?>>

Assert.IsInstanceOfType(result, typeof(Expression<Func<Foo, object?>>));

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.