Coder Social home page Coder Social logo

Comments (6)

codingseb avatar codingseb commented on June 22, 2024

Hello @trrahul.
No the output is right.

This because a variable contains a C# object and in your case. variables c and d contains a string. So they are interpreted as string.
So if you do this :

evaluator.Evaluate("c+d");
// or if
evaluator.Variables["h"] = "Hello";
evaluator.Variables["w"] = "World";
evaluator.Evaluate("h+\" \" +w");

you get :

a+bc+3
Hello World

but if you want to evaluate a string in a evaluation you could do something like this :

evaluator.Evaluate("Evaluate(c)");
evaluator.Evaluate("Evaluate(d)");

And then you will get :

3
6

See Custom variables and Functions for more infos

from expressionevaluator.

codingseb avatar codingseb commented on June 22, 2024

After a little thought. I think it might be good to introduce a type of variable that could be evaluated when encountered to better answer to this issue.
Something like :

evaluator.Variables["c"] = new SubExpression("a+b");
evaluator.Variables["d"] = new SubExpression("c+3");

I Reopen this issue and will work on it for the next version of ExpressionEvaluator.
So @trrahul thanks for this issue.

from expressionevaluator.

trrahul avatar trrahul commented on June 22, 2024

Isn't it better to search for the variables in the expression and evaluate them in order instead of creating a new subexpression?

Because in this case, please correct me if I am wrong, every variable has to be created as a subexpression if they are used later in another expression.

from expressionevaluator.

codingseb avatar codingseb commented on June 22, 2024

You need to understand that variables are just containers like C# variables or any programming language variables. It could contains anything (string, int, float, Regex, File or a custom class...) and a string is not a expression it's a string and it's right like this because you can do operation with string variables so you need a specific type to specify that the content of a variable must be evaluate otherwise the variable is simply evaluated as it's content.

Because in this case, please correct me if I am wrong, every variable has to be created as a subexpression if they are used later in another expression.

No if a variable is just a value like your variables a or b (int) they are interpreted as this.
So only variables that need to be evaluated when met need to be contains in a specific type that ExpressionEvaluator know it' need to be sub evaluate.

But of course if you also encapsulate a and b with subexpression it will also work. Because the subevaluation will return their values.

from expressionevaluator.

trrahul avatar trrahul commented on June 22, 2024

Thanks for the clarification. I was trying out different expression evaluators and this issue happened with all the libs I tried except lambdaparser. It was able to evaluate the expression correctly, but it had another problem ExpressionEvaluator did not have. nreco/lambdaparser#22

from expressionevaluator.

codingseb avatar codingseb commented on June 22, 2024

I just published version 1.4.9.0 that allow to the use of SubExpression here is the doc on the wiki SubExpression variable

from expressionevaluator.

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.