create case(variable, expression_list) function#52
create case(variable, expression_list) function#52EduardoRSeifert wants to merge 13 commits intomasterfrom
Conversation
|
Hmmm, the
Maybe if we break it into single responsibilities it would be easier to comprehend. For example
Even simpler approachWe can use the number(case("5", "@ < 6;5*5")) |
You misunderstood. @ means 2 different things, yes, but it always means the same thing within it's argument. I used the same token to avoid creating a new one @ in the comparison means "substitute @ for the variable and evaluate the resulting equation", while @ in the return value means "evaluate the return value as an equation". So case("blabla.blabla", "@ > 5;@10", "default;@8") will result in "blabla.blabla > 5 ? 10 : 8". But don't mistake @10 as cating to integer, it means "evaluate 10 as an equation", but ot RESULTS in the same as casting to integer. Hope that made it clearer. Yes, you could use case("blabla.blabla", "@ > 5;@Number(10)", "default;@Number(8)") to be more explicit that you want a number return, but is that necessary? |
|
@EduardoRSeifert Got it! case("5", "@ < 6;@5*5", "default;@666")Means case 5
when < 6
5 * 5
else
666
endcase("edimar", "xablau;nao", "edimar;sim")Means case "edimar"
when "xablau"
"nao"
when "edimar"
"sim"
end |
Victorcorcos
left a comment
There was a problem hiding this comment.
There are conflicts in tests.sh now 👍
There was a problem hiding this comment.
Very interesting and useful function!
I'm just waiting for @niltonvasques approval here.
This PR creates the case function.
It receives a variable as the first parameter, then a list of comparisons and results as the other parameters
Syntax
The list should be in this syntax:
Using the @ token
Variable will be treated as a string, but can be considered a number when doing an operation in the comparison. example:
To use the variable in a comparison operation, we use the @ token. It is also used in the result to evaluate the result instead of returning a string
Default value
It is NOT strictly needed, but should be excluded carefully. Good example of removing it:
case("5", "@<4;do this", "@>=4;do that")since the combination of the comparisons is a Tautologycase("5", "@<5;do this", "@>5;do that")herein lies the problem: 5 is neither < 5 nor > 5. Make sure there is a Tautology in the comparisons if the default value is not added (the parser will throw an error if no comparisons are satisfied and there isn't a default)case("5", "@<5;do this", "@>5;do that", "default;something wrong happened")is the safest way to construct the caseTests