Flee icon indicating copy to clipboard operation
Flee copied to clipboard

ternary operation support?

Open cdarrigo opened this issue 3 years ago • 3 comments

The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false.

Is this type of operation supported in Flee?

cdarrigo avatar Aug 19 '22 15:08 cdarrigo

Have you actually tried such an operation and see what you get?

shawndewet avatar Sep 22 '22 13:09 shawndewet

Seems it's not supported:

 [Fact]
        public void Expression_Ternary_Operator()
        {
            ExpressionContext expressionContext = new();
            expressionContext.Imports.AddType(typeof(Math));

            expressionContext.Variables["operation"] = true; //or subtract

            IGenericExpression<int> eGeneric = expressionContext.CompileGeneric<int>("operation ? 1+3 : 1-3");
            var actual = eGeneric.Evaluate();

            actual.ShouldBe(4);
        }

The above fails with the following error: Flee.Parsing.ParserLogException : unexpected character '?', on line: 1 column: 11

shawndewet avatar Sep 29 '22 17:09 shawndewet

When I needed a ternary function, I wrote a simple custom method named IIf (boolExpression, trueValue, falseValue) that returned trueValue when boolExpression evaluated to true, and falseValue when it evaluated to false.

viklele avatar Jul 06 '23 10:07 viklele