mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-02 13:41:10 +00:00
1 KiB
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