erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.7k stars 55 forks source link

Add new `do`/anonymous function block syntax #528

Open mtshiba opened 2 months ago

mtshiba commented 2 months ago

We propose new syntax for do/anonymous function block,

do { ... }
i -> { ... }

which is equivalent to

do:
    ...
i ->
   ...

This syntax makes it easy to pass blocks to a function.

foo do { ... }, i -> { ... }, j -> {
    ...
}, k -> {
... # no indentation allowed
}

The { immediately after do, ->(=>) is not considered to be the { of a brace container (set, record, dict).

i -> { i + 1 } # : Nat -> Nat
i -> { {i + 1} } # : Nat -> Set Nat
k = 3
do { i = 1; j = 2; k } # : () -> Nat
do { { i = 1; j = 2; k } } # : () -> { i = Nat; j = Nat; k = Nat }