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

The == operator only compares the reference #661

Open
Yaming-Hub opened this issue May 25, 2024 · 1 comment
Open

The == operator only compares the reference #661

Yaming-Hub opened this issue May 25, 2024 · 1 comment

Comments

@Yaming-Hub
Copy link

The default == and != operator compare equality by reference. So when I have classes overrides Equals() method, these customized equal logic won't be honored. The same issue also impacts contains binary operator. Ideally when compare objects or check if an array contains an object, we should compare value instead of reference.

@Yaming-Hub
Copy link
Author

Right now I work around this problem by defining my own ValueEqualBinaryExpression class and overwrite the "==" operator in parser.

    public sealed class ValueEqualBinaryExpression : BinaryExpressionBase
    {
        public ValueEqualBinaryExpression(Expression left, Expression right) : base(left, right)
        {
        }


        protected override FluidValue Evaluate(FluidValue leftValue, FluidValue rightValue, TemplateContext context)
        {            
            if (leftValue.Type == FluidValues.Object && rightValue.Type == FluidValues.Object)
            {
                var left = leftValue.ToObjectValue();
                var right = rightValue.ToObjectValue();
                return left.Equals(right)
                    ? BooleanValue.True
                    : BooleanValue.False;
            }

            return leftValue.Equals(rightValue) 
                ? BooleanValue.True
                : BooleanValue.False;
        }
    }

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

1 participant