Created
February 3, 2021 13:35
-
-
Save leveled/03c75e58101aed8a2792bdd9f6918061 to your computer and use it in GitHub Desktop.
Modules in nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Module A | |
var | |
x*, y: int | |
proc `*` *(a, b: seq[int]): seq[int] = | |
# allocate a new sequence: | |
newSeq(result, len(a)) | |
# multiply two int sequences: | |
for i in 0..len(a)-1: result[i] = a[i] * b[i] | |
when isMainModule: | |
# test the new ``*`` operator for sequences: | |
assert(@[1, 2, 3] * @[1, 2, 3] == @[1, 4, 9]) | |
# Module C | |
import A, B | |
write(stdout, x) # error: x is ambiguous | |
write(stdout, A.x) # okay: qualifier used | |
var x = 4 | |
write(stdout, x) # not ambiguous: uses the module C's x | |
import mymodule except y | |
from mymodule import x, y, z | |
from mymodule as m import nil | |
m.x() # m is aliasing mymodule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment