erg/doc/EN/syntax/08_procedure.md
2022-12-28 13:40:16 +09:00

1 KiB

Procedures

Procedures mean the functions that allow side-effect. Please refer to Function basically usage or definition. Add ! to a function name to define it.

proc!(x: Int!, y: Int!) =
    for! 0..x, i =>
        for 0..y, j =>
            print! i, j

Procedures are necessary when dealing with mutable objects, but having a mutable object as an argument does not necessarily make it a procedure. Here is a function takes a mutable object (not procedure).

peek_str s: Str! = log s

make_proc(x!: (Int => Int)): (Int => Int) = y => x! y
p! = make_proc(x => x)
print! p! 1 # 1

Also, procedures and functions are related by proc :> func. Therefore, it is possible to define functions in procedure. However, note that the reverse is not possible.

proc!(x: Int!) = y -> log x, y # OK
func(x: Int) = y => print! x, y # NG

Previous | Next