aboutsummaryrefslogtreecommitdiffstats
path: root/examples/impure-concept.milch
blob: bf4f63bd8a0b28470e57578d0ece7ae01912eee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(import "core/common")

; What color is your function?
; Impure functions are functions that can call both pure and impure functions.
; Pure functions can only call pure functions.
; Impure function names end in an exclamation mark (!) by convention.
; The top-level context is impure but has special rules
;   related to `import`, `let`, and other builtins.

; this is a pure function
(let f (\[x] x))

(f 10) ; works

; this is an impure function
(let g! (\![x] (print-fmt! "{0}\n" [x])))

(g! "foo") ; works

; this is a pure function that tries to call an impure function
(let h (\[x] (g! x)))

(h "bar")
; error: cannot call impure function in pure context
;  when calling function g! at examples/impure-concept.milch:21:14
;  in a function definition at examples/impure-concept.milch:21:11
;  when calling function h at examples/impure-concept.milch:23:1