diff options
author | Araq <rumpf_a@web.de> | 2012-08-16 08:34:33 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2012-08-16 08:34:33 +0200 |
commit | 0171566c98d7341ef6c4c36a1d056b19e24f681e (patch) | |
tree | dd211866c615402ae1f380d1ed79eb1cef27eb1b /doc | |
parent | 12151930101f6eacb834c2102cfdaccc637ce72a (diff) | |
download | Nim-0171566c98d7341ef6c4c36a1d056b19e24f681e.tar.gz |
openarray/varargs split; breaks bootstrapping
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/manual.txt | 60 | ||||
-rwxr-xr-x | doc/tut1.txt | 27 |
2 files changed, 62 insertions, 25 deletions
diff --git a/doc/manual.txt b/doc/manual.txt index 665ca265c..ed80984d7 100755 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -890,24 +890,44 @@ The notation ``x[i]`` can be used to access the i-th element of ``x``. Arrays are always bounds checked (at compile-time or at runtime). These checks can be disabled via pragmas or invoking the compiler with the ``--boundChecks:off`` command line switch. + +The current implementation does not support nested open arrays. -An open array is also a means to implement passing a variable number of -arguments to a procedure. The compiler converts the list of arguments -to an array automatically: - -.. code-block:: nimrod - proc myWriteln(f: TFile, a: openarray[string]) = - for s in items(a): - write(f, s) - write(f, "\n") - - myWriteln(stdout, "abc", "def", "xyz") - # is transformed by the compiler to: - myWriteln(stdout, ["abc", "def", "xyz"]) - -This transformation is only done if the openarray parameter is the -last parameter in the procedure header. The current implementation does not -support nested open arrays. + +Varargs +~~~~~~~ + +A `varargs`:idx: parameter is an openarray parameter that additionally +allows to pass a variable number of arguments to a procedure. The compiler +converts the list of arguments to an array implicitely: + +.. code-block:: nimrod + proc myWriteln(f: TFile, a: varargs[string]) = + for s in items(a): + write(f, s) + write(f, "\n") + + myWriteln(stdout, "abc", "def", "xyz") + # is transformed by the compiler to: + myWriteln(stdout, ["abc", "def", "xyz"]) + +This transformation is only done if the varargs parameter is the +last parameter in the procedure header. It is also possible to perform +type conversions in this context: + +.. code-block:: nimrod + proc myWriteln(f: TFile, a: varargs[string, `$`]) = + for s in items(a): + write(f, s) + write(f, "\n") + + myWriteln(stdout, 123, "abc", 4.0) + # is transformed by the compiler to: + myWriteln(stdout, [$123, $"def", $4.0]) + +In this example ``$`` is applied to any argument that is passed to the +parameter ``a``. (Note that ``$`` applied to strings is a nop.) + Tuples and object types @@ -2336,8 +2356,7 @@ Var parameters The type of a parameter may be prefixed with the ``var`` keyword: .. code-block:: nimrod - proc divmod(a, b: int, - res, remainder: var int) = + proc divmod(a, b: int; res, remainder: var int) = res = a div b remainder = a mod b @@ -2355,8 +2374,7 @@ an l-value. Var parameters are implemented as hidden pointers. The above example is equivalent to: .. code-block:: nimrod - proc divmod(a, b: int, - res, remainder: ptr int) = + proc divmod(a, b: int; res, remainder: ptr int) = res[] = a div b remainder[] = a mod b diff --git a/doc/tut1.txt b/doc/tut1.txt index 78f5dfecd..a5554cffc 100755 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -1131,12 +1131,17 @@ an openarray parameter, the index type does not matter. The openarray type cannot be nested: multidimensional openarrays are not supported because this is seldom needed and cannot be done efficiently. -An openarray is also a means to implement passing a variable number of + +Varargs +------- + +A ``varargs`` parameter is like an openarray parameter. However, it is +also a means to implement passing a variable number of arguments to a procedure. The compiler converts the list of arguments to an array automatically: .. code-block:: nimrod - proc myWriteln(f: TFile, a: openarray[string]) = + proc myWriteln(f: TFile, a: varargs[string]) = for s in items(a): write(f, s) write(f, "\n") @@ -1145,8 +1150,22 @@ to an array automatically: # is transformed by the compiler to: myWriteln(stdout, ["abc", "def", "xyz"]) -This transformation is only done if the openarray parameter is the -last parameter in the procedure header. +This transformation is only done if the varargs parameter is the +last parameter in the procedure header. It is also possible to perform +type conversions in this context: + +.. code-block:: nimrod + proc myWriteln(f: TFile, a: varargs[string, `$`]) = + for s in items(a): + write(f, s) + write(f, "\n") + + myWriteln(stdout, 123, "abc", 4.0) + # is transformed by the compiler to: + myWriteln(stdout, [$123, $"def", $4.0]) + +In this example ``$`` is applied to any argument that is passed to the +parameter ``a``. Note that ``$`` applied to strings is a nop. Tuples |