From 8397eac55b57299aeb4a98a46e377ab7f40bdbef Mon Sep 17 00:00:00 2001 From: flaviut Date: Wed, 16 Jul 2014 15:14:04 -0400 Subject: [PATCH] Partial work on procs --- content/procs.md | 60 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/content/procs.md b/content/procs.md index 775bb8b..93a9d6b 100644 --- a/content/procs.md +++ b/content/procs.md @@ -1,7 +1,7 @@ --- -title: Procedures +title: Procs --- -# Procedures +# Procs Procedures in Nimrod are declared using `proc` and require that their parameter and return types be annotated. After the types and parameters, an `=` is used to denote the start of the function body. @@ -11,4 +11,58 @@ proc fibonacci(n: int): int = result = n else: result = fibonacci(n - 1) + fibonacci(n - 2) -``` \ No newline at end of file +``` + +## Exporting symbols + + +Encapsulation is also supported, not by conventions such as perpending the name with underscores but by annotating a procedure with `*`, which exports it and makes it available for use by modules. + +``` nimrod +# module1: +proc foo*(): int = 2 +proc bar(): int = 3 + +# module2: +echo foo() # Valid +echo bar() # will not compile +``` + +## Side effect analyses + +Nimrod provides support for functional programming and so includes the `{.noSideEffect.}` pragma, which statically ensures there are no side effects. + +``` nimrod +proc sum(x, y: int): int {. noSideEffect .} = + x + y + +proc minus(x, y: int): int {. noSideEffect .} = + echo x # error: 'minus' can have side effects + x - y +``` + +## Operators + +To create an operator, the symbols that are to be used must be encased inside `` ` ``s to signify they are operators. + +``` nimrod +proc `$`(a: array[2, array[2, int]]): string = + result = "" + for y in a: + for vx in v: + result.add($vx & ", ") + result.add("\n") + +echo [[1, 2], # See Varargs for how + [3, 4]] # echo works + +proc `^&*^@%`(a, b: string): string = + ## An confusingly named useless proc + result = a[0] & b[high(str)] + +assert("foo" ^&*^@% "bar" == "fr") +``` + + +- operators +- generics \ No newline at end of file