diff options
author | Zahary Karadjov <zahary@gmail.com> | 2013-12-29 17:21:00 +0200 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2013-12-29 17:21:00 +0200 |
commit | afddae5aaf08a3a3357ec33d0bc82bdba0f5dc08 (patch) | |
tree | b6bd783f813797dcb5be4abaaade5ad599bb1c5c /doc | |
parent | 437cfa73abd8fdf878cc2af2c44acbc4b6ec3a56 (diff) | |
parent | 72291875bf895e8e0d22ab3f375752417b07ed25 (diff) | |
download | Nim-afddae5aaf08a3a3357ec33d0bc82bdba0f5dc08.tar.gz |
Merge branch 'upstream' into devel
Conflicts: compiler/ccgutils.nim compiler/msgs.nim compiler/sem.nim compiler/semexprs.nim compiler/seminst.nim compiler/semmagic.nim compiler/semstmts.nim compiler/semtypes.nim compiler/semtypinst.nim compiler/sigmatch.nim compiler/types.nim compiler/vmgen.nim lib/core/macros.nim lib/system.nim tests/reject/tenummix.nim web/news.txt
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual.txt | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/doc/manual.txt b/doc/manual.txt index 5024fffec..2d8feca17 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -3837,6 +3837,60 @@ This is a simple syntactic transformation into: Special Types ============= +static[T] +--------- + +As their name suggests, static params must be known at compile-time: + +.. code-block:: nimrod + + proc precompiledRegex(pattern: static[string]): TRegEx = + var res {.global.} = re(pattern) + return res + + precompiledRegex("/d+") # Replaces the call with a precompiled + # regex, stored in a global variable + + precompiledRegex(paramStr(1)) # Error, command-line options + # are not known at compile-time + + +For the purposes of code generation, all static params are treated as +generic params - the proc will be compiled separately for each unique +supplied value (or combination of values). + +Furthermore, the system module defines a `semistatic[T]` type than can be +used to declare procs accepting both static and run-time values, which can +optimize their body according to the supplied param using the `isStatic(p)` +predicate: + +.. code-block:: nimrod + + # The following proc will be compiled once for each unique static + # value and also once for the case handling all run-time values: + + proc re(pattern: semistatic[string]): TRegEx = + when isStatic(pattern): + return precompiledRegex(pattern) + else: + return compile(pattern) + +Static params can also appear in the signatures of generic types: + +.. code-block:: nimrod + + type + Matrix[M,N: static[int]; T: Number] = array[0..(M*N - 1), T] + # Please, note how `Number` is just a type constraint here, while + # `static[int]` requires us to supply a compile-time int value + + AffineTransform2D[T] = Matrix[3, 3, T] + AffineTransform3D[T] = Matrix[4, 4, T] + + AffineTransform3D[float] # OK + AffineTransform2D[string] # Error, `string` is not a `Number` + + typedesc -------- |