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

Does Expr have Control flow #601

Answered by antonmedv
pat3icki asked this question in General
Discussion options

Hey, good day am trying to integrate this into a project as a middleware for the project, I predicted the script is gonna get a little complex, is there a way to have if statements or if-else, I can’t find any documentation or issues about this.
Thanks

You must be logged in to vote

Now Expr has control flows!

let foo = if bar > 0 { 
 true
} else {
 false
}

Replies: 10 comments 11 replies

Comment options

As far as I know, Expr only has the ternary statement:

valueA != valueB ? do-if-true : do-if-false

It's in the Operators.

You must be logged in to vote
0 replies
Comment options

Yeap. But I do want to implement if statements as well.

You must be logged in to vote
0 replies
Comment options

If it could support control flow and custom functions, that would be great.

You must be logged in to vote
1 reply
Comment options

Custom functions already supported.

Comment options

maybe we can implement a simple "pattern matching" first?

match a {
 1 -> "foo"
 2 -> "bar"
 default -> "none"
}

or

switch {
case <condition 1> -> "foo"
case <condition 2> -> "bar"
default -> "none"
}

something like aboves is really help

You must be logged in to vote
0 replies
Comment options

BTW: currently I use go template + sprig for implement my business logic, it's too heavy and a bit slow than expr. The blocker of my expr migration is if statement or pattern matching.

You must be logged in to vote
3 replies
Comment options

So what king of the problem are you trying to solve? Please, more context.

Comment options

Here an example of my side project, https://github.com/phuslu/liner/blob/master/example.yaml#L47-L66 , please the "policy" and "dialer" field.

Comment options

Comment options

Would greatly benefit control flow as well. Either switch case or if/else are good options for my use-cases. I can work around with ternary however, just less practical.

You must be logged in to vote
0 replies
Comment options

@antonmedv Any plans on having if-else in near future? It's easier for business folks as well to use

You must be logged in to vote
1 reply
Comment options

Yes, we are planning to implement if-else in near future. I guess this will be a new feature after current release.

Comment options

I'm facing a scenario where if-else proves very helpful, consider this script:

if input1 != "" {
 return input1
}
b := calculateB(input2);
c := calculateC();
return getResult(b, c);

To do this in go expr, I'm currently planning to write this:

let b = input1 == "" ? calculateB(input2) : nil;
let c = input1 == "" ? calculateC() : nil;
input1 != "" ? input1 : getResult(b, c)

It would be so much nicer if the expression language has a "return" keyword, or a rust-like if-else expression:

if input1 != "" {
 input1
} else {
 let b = calculateB(input2);
 let c = calculateC();
 getResult(b, c)
}

I would say this is easier to read.

You must be logged in to vote
3 replies
Comment options

True! This is why we are working on it. ping @OlgaNovg ;)

Comment options

#736
Looks like this pull request has been open few months ago. Is there a path forward?
(I'm evaluating expr for a use case which needs control flow)

Comment options

Yes! @OlgaNovg is also waiting long time already)

Comment options

Hey folks - excited that this is now merged! Is there a timeline for the next release?

You must be logged in to vote
1 reply
Comment options

Soon! I have to fix a few issues before. And i want to add support for sequence of expressions.

Comment options

Now Expr has control flows!

let foo = if bar > 0 { 
 true
} else {
 false
}
You must be logged in to vote
2 replies
Comment options

where is the documented?

Comment options

you can read this in 1.17.0 release note.

Very small and short, but there is a document too. (Maybe no one made a detailed document PR yet)
스크린샷 2025年08月19日 오전 11 55 49

Answer selected by antonmedv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #599 on March 12, 2024 14:04.

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