Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support for statements in expr? #566

Answered by antonmedv
PranavPeshwe asked this question in General
Discussion options

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?

You must be logged in to vote

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

Comment options

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
You must be logged in to vote
0 replies
Comment options

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!

You must be logged in to vote
2 replies
Comment options

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.

Answer selected by PranavPeshwe
Comment options

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /