You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An if used as a value does not parse when its condition ends in a call: s = if f(x) { a } else { b } #3478
An if used as a value does not parse when its condition ends in a call:
yes(x: int) -> bool -> x > 0
main() {
s = if yes(1) { "yes" } else { "no" }
println(s)
}
error[E0100]: Expected LEFT_BRACE, got ELSE
--> a.ae:4:29
4 | s = if yes(1) { "yes" } else { "no" }
Expected: it prints yes. The language reference and examples/01-language/04-control-flow.ae say if c { a } else { b } is a value whatever c is.
The same condition works as an if statement (if yes(1) { s = "yes" }), and in an if expression when parenthesized (if (yes(1)) { ... }), so it looks like the expression form's condition is parsed with the trailing-block rule: yes(1) { "yes" } taken as a call passed a block. A condition with a call that is not last parses too (if yes(1) && x > 0 { ... } else { ... } is fine; if 1 < 2 && yes(1) { 1 } else { 2 } fails the same way).
Found writing the Rosetta Code tranche of #1191 (PR #3477): Roman numerals/Decode (total += if i + 1 < n && v < worth(roman, i + 1) { -v } else { v }) and Leap year (kind = if leap(year) { "a leap year" } else { "not a leap year" }) wait for it, written without the parentheses. Filed for the next batch.
Activity
examples: a first Rosetta Code tranche, measured against Go and Python (
fix: an if used as a value parses when its condition ends in a call (#…
fix: a call's arguments parse as anywhere inside a condition (#3478)
examples: Roman numerals/Decode, Leap year and Caesar cipher (aether-…