Skip to content

An if used as a value does not parse when its condition ends in a call: s = if f(x) { a } else { b } #3478

Closed

Description

@nicolas-maman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions