Bend
Bend copied to clipboard
(Request) If expressions
At the moment, all pattern matching syntax happens only at the statement level. This makes writing some stuff a pain.
For example, if we want to write a max function with a lambda, that's not possible.
I propose the addition of python-like if expressions:
lambda a, b: a if a > b else b
This is ok, but opens the can of worms of what statements should have expression versions, which i'd like to be 0.
Alternatives:
- Current way (users have to define a separate function)
- Scoped functions. We can restrict them to always be combinators and implicitly pass captured variables, which would avoid duplication problems (we'd catch it during runtime).
def FnOuter(a):
def FnInner(b):
return f(a, b)
return FnInner(2)
I'd like to propose two seemingly unconsidered alternatives, each of which would fix the expression-statement issue:
- A C-like ternary operator
Imperative syntax:
condition ? is_true : is_falseFunctional syntax:(? condition is_true is_false) - An
ifelsefunction. Imperative syntax:ifelse(condition, is_true, is_false)Functional syntax:(ifelse condition is_true is_false)
The C-like option would be less Python-like, but would still be familiar to most developers and would be more concise. On the other hand, the ifelse option would be more Python-like, but would run the risk of potentially breaking programs that have an ifelse in their namespace.