Repository navigation
Expand file tree
/
Copy pathmain.fl
More file actions
50 lines (44 loc) · 1.82 KB
/
Copy pathmain.fl
File metadata and controls
50 lines (44 loc) · 1.82 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# modules: a multi-file program.
#
# run it from anywhere; the imports resolve against this file, not against
# the directory you happen to be standing in:
#
# ./flint examples/modules/main.fl
#
# the four files are:
#
# shapes.fl the data and the functions
# stats.fl a second module, importing the first
# limits.fl a module exporting a const
# main.fl this file: the entry point, imports the other three
#
# every file each module defines that is marked `export`, reached through
# the name the import binds. a module's unexported names are private: two
# modules can each have a helper of the same name and neither sees the
# other's.
import "shapes.fl" as shapes_mod
import "stats.fl" as stats
import "limits.fl" as limits
let shapes = [
{kind: "rect", w: 3, h: 4},
{kind: "circle", r: 1}
]
print(shapes_mod.describe(shapes[0]))
print(shapes_mod.describe(shapes[1]))
print("total: " + str(stats.total_area(shapes)))
# `largest` is also from stats.fl. it uses `a > best_area` internally rather
# than `if a`, because 0 is truthy and testing the number would take the
# branch on the first iteration regardless of the areas.
let biggest = stats.largest(shapes)
print("largest is a " + biggest.kind)
# shapes.fl has a private `normalise`. it is not exported, so reading it
# through the module gives nil rather than the function. that is the whole
# of module privacy: two modules can each have a `scale` or a `round` and
# neither can see or overwrite the other's.
print(shapes_mod.normalise)
# const works across the boundary: a const is a property of the binding in
# the module that declared it, and the importer cannot write to a binding it
# does not own.
print("limit is " + str(limits.LIMIT))
print("clamp(500) is " + str(limits.clamp(500)))
# limits.LIMIT = 99 # error: cannot assign to constant 'LIMIT'