documented 'mixin' declaration

This commit is contained in:
Araq 2012-11-08 08:32:08 +01:00
commit ed28f3c8dc
4 changed files with 36 additions and 7 deletions

View file

@ -692,4 +692,7 @@ Is transformed to:
let i = foo(cl) let i = foo(cl)
if cl.state == -1: break if cl.state == -1: break
""" """
InternalAssert body.kind == nkForStmt
# gather vars in a tuple:
var L = body.len

View file

@ -2778,7 +2778,12 @@ This allows for a Lisp-like `condition system`:idx:\:
var myFile = open("broken.txt", fmWrite) var myFile = open("broken.txt", fmWrite)
try: try:
onRaise(proc (e: ref E_Base): bool = onRaise(proc (e: ref E_Base): bool =
stdout.writeln "ok, writing to stdout instead") if e of EIO:
stdout.writeln "ok, writing to stdout instead"
else:
# do raise other exceptions:
result = true
)
myFile.writeln "writing to broken file" myFile.writeln "writing to broken file"
finally: finally:
myFile.close() myFile.close()
@ -2859,6 +2864,9 @@ effect is a means to *tag* a routine and perform checks against this tag:
# the compiler prevents this: # the compiler prevents this:
let x = readLine() let x = readLine()
A tag has to be a type name. A ``tags`` list - like a ``raises`` list - can
also be attached to a proc type. This affects type compatibility.
The inference for tag tracking is analogous to the inference for The inference for tag tracking is analogous to the inference for
exception tracking. exception tracking.
@ -2948,6 +2956,7 @@ Example:
`type parameters`:idx:. Depending on context, the brackets are used either to `type parameters`:idx:. Depending on context, the brackets are used either to
introduce type parameters or to instantiate a generic proc, iterator or type. introduce type parameters or to instantiate a generic proc, iterator or type.
Is operator Is operator
~~~~~~~~~~~ ~~~~~~~~~~~
@ -3084,6 +3093,8 @@ To be written.
Return Type Inference Return Type Inference
~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
**Note**: ``auto`` is not yet implemented.
If a type class is used as the return type of a proc and it won't be bound to If a type class is used as the return type of a proc and it won't be bound to
a concrete type by some of the proc params, Nimrod will infer the return type a concrete type by some of the proc params, Nimrod will infer the return type
from the proc body. This is usually used with the ``auto`` type class: from the proc body. This is usually used with the ``auto`` type class:
@ -3098,12 +3109,17 @@ Future versions of nimrod may also support overloading based on the return type
of the overloads. In such settings, the expected result type at call sites may of the overloads. In such settings, the expected result type at call sites may
also influence the inferred return type. also influence the inferred return type.
Symbol lookup in generics Symbol lookup in generics
~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
Symbols in generics are looked up in two different contexts: Both the context The symbol binding rules in generics are slightly subtle: There are "open" and
at definition and the context at instantiation are considered for any symbol "closed" symbols. A "closed" symbol cannot be re-bound in the instantiation
occurring in a generic: context, an "open" symbol can be. Per default overloaded symbols are open
and every other symbol is closed.
Open symbols are looked up in two different contexts: Both the context
at definition and the context at instantiation are considered:
.. code-block:: nimrod .. code-block:: nimrod
type type
@ -3122,6 +3138,16 @@ the ``TIndex`` type is defined *after* the ``==`` for tuples; yet the example
compiles as the instantiation takes the currently defined symbols into account compiles as the instantiation takes the currently defined symbols into account
too. too.
A symbol can be forced to be open by a `mixin`:idx: declaration:
.. code-block:: nimrod
proc create*[T](): ref T =
# there is no overloaded 'mixin' here, so we need to state that it's an
# open symbol explicitly:
mixin init
new result
init result
Templates Templates
--------- ---------

View file

@ -11,11 +11,10 @@ version 0.9.2
- ``hoist`` pragma for loop hoisting: can be easily done with - ``hoist`` pragma for loop hoisting: can be easily done with
AST overloading + global AST overloading + global
- document 'mixin' for generics and symbol lookup rules; special rule
for ``[]=``
- implement the compiler as a service - implement the compiler as a service
- ``=`` should be overloadable; requires specialization for ``=`` - ``=`` should be overloadable; requires specialization for ``=``
- make 'bind' default for templates and introduce 'mixin' - make 'bind' default for templates and introduce 'mixin';
special rule for ``[]=``
- implicit deref for parameter matching; overloading based on 'var T' - implicit deref for parameter matching; overloading based on 'var T'
- optimize genericAssign in the code generator - optimize genericAssign in the code generator

View file

@ -35,6 +35,7 @@ Language Additions
- Table constructors now mimic more closely the syntax of the ``case`` - Table constructors now mimic more closely the syntax of the ``case``
statement. statement.
- Nimrod can now infer the return type of a proc from its body. - Nimrod can now infer the return type of a proc from its body.
- Added a ``mixin`` declaration to affect symbol binding rules in generics.
- Exception tracking has been added and the ``doc2`` command annotates possible - Exception tracking has been added and the ``doc2`` command annotates possible
exceptions for you. exceptions for you.