-
-
Couldn't load subscription status.
- Fork 463
Support for statements in expr? #566
-
Hi,
As I understand, exprlang only supports evaluating a single expression(which could be arbitrarily complicated) to generate a bool/map as the result. Is there a plan to support statements (and consequently a return or equivalent keyword)?
Here is a cooked-up example(in pseudocode) of what I mean to say:
a=<a complicated expression referring multiple vars in the env utilising nested conditionals>;
b=<another complicated expression referring multiple vars in the env utilising nested conditionals>;
if a==true && (env.varA > env.varB) c=env.varC else c=env.varD;
if b==true && (env.varE > env.varF) return {b,c} else return {env.varG,false}
I know for a fact that with enough perseverance even the most complicated conditions & return values can be represented as [extremely] nested ternary expressions and fed to the expr VM. But those are not maintenance friendly and are hard to debug & test during development.
Being able to break such conditions down into statements will go a long way to mitigate that problem and add a lot of expressive power to the lang.
WDYT?
Beta Was this translation helpful? Give feedback.
All reactions
Yes, this is something that we can also add the the language. Also assignment operator will be needed. And we need to decide if only vars can be assigned or env values as well.
Replies: 2 comments 2 replies
-
Yes! This is one of the things I'm definitely looking to add into expr. I discussed this topic with several big companies which have huge expr expressions.
I'm thinking to add support for if expressions and return statement.
First thing is to add alternative for ternary operator cond ? expr1 : expr2 as an if:
if cond { expr1 } else { expr2 }
I'd like to keep syntax for if close to go, but if should still be an expression.
let var = if cond > 0 { "success" } else { "also success" }
With if as expression, else is required. But with combination with return, else can be omitted:
if cond > 0 { return "success" } foo + bar
Beta Was this translation helpful? Give feedback.
All reactions
-
Hey @antonmedv, Thanks for replying!
Good to hear that statements are on the cards.
Are you planning to support something like this as well:
if cond > 0 {
varA = 123
varB = 987
} else {
varB = 456
varC = 678
}
// then use varA, varB & varC in later code
Basically, we need not return from the if and each if is not bound to return a single value (like you wrote earlier with let var = if ...).
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, this is something that we can also add the the language. Also assignment operator will be needed. And we need to decide if only vars can be assigned or env values as well.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks!
Beta Was this translation helpful? Give feedback.