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

Support for implicit operators #880

Open
victorr99 opened this issue Jan 24, 2025 · 1 comment
Open

Support for implicit operators #880

victorr99 opened this issue Jan 24, 2025 · 1 comment
Assignees

Comments

@victorr99
Copy link

I would like to submit a feature request to add support for implicit operators.

To my understanding, implicit operators are only supported for booleans at the moment. We have a use case were we need to support implicit operators to cast from and to a structure.

We would like to be able to run the following example:

class Program
{
    static void Main(string[] args)
    {
        var parser = new ExpressionParser(
            [],
            "MyMethods.UsesMyStruct(\"Foo\")",
            [],
            new ParsingConfig());
        
        var expression = parser.Parse(typeof(MyStruct));
        var lambda = Expression.Lambda<Func<MyStruct>>(expression);
        var method = lambda.Compile();

        var result = method();
        // result == MyStruct with Value = "Foo"
    }

    [DynamicLinqType]
    public static class MyMethods
    {
        public static string UsesMyStruct(MyStruct myStruct)
        {
            return myStruct.Value;
        }
    }

    public readonly struct MyStruct(string value)
    {
        private readonly string _value = value;

        public static implicit operator MyStruct(string value)
        {
            return new MyStruct(value);
        }
        
        public static implicit operator string(MyStruct myStruct)
        {
            return myStruct._value;
        }
        
        public string Value => _value;
    }
}

In plain C# this is also possible:

string result = MyMethods.UsesMyStruct("foo");

Running the example above results in the following exception:

Unhandled exception. No applicable method 'UsesMyStruct' exists in type 'MyMethods' (at index 10)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMemberAccess(Type type, Expression expression, String id)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseTypeAccess(Type type, Boolean getNext)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIdentifier()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimaryStart()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseUnary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseArithmetic()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAdditive()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseShiftOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor)
@StefH StefH self-assigned this Jan 24, 2025
@StefH StefH added feature and removed feature labels Jan 24, 2025
@git-bone
Copy link

I would like this feature as well, this would be a great addition to the library

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

No branches or pull requests

3 participants