diff options
author | Zahary Karadjov <zahary@gmail.com> | 2014-02-15 17:41:35 +0200 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2014-02-15 17:41:35 +0200 |
commit | 492fa86638f20c3230d9086296b9d1c76ae66916 (patch) | |
tree | 9b22185affba6b0717e2ab7940e48dcf2129859f /doc/manual.txt | |
parent | a158053ae9d04ebd882b2c973ddf4a3dd7d4efe8 (diff) | |
download | Nim-492fa86638f20c3230d9086296b9d1c76ae66916.tar.gz |
the delegator pragma becomes a set of dot operators
Diffstat (limited to 'doc/manual.txt')
-rw-r--r-- | doc/manual.txt | 89 |
1 files changed, 53 insertions, 36 deletions
diff --git a/doc/manual.txt b/doc/manual.txt index fb357f7d3..53817c508 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -4106,6 +4106,59 @@ types that will match the typedesc param: The constraint can be a concrete type or a type class. +Special Operators +================= + +dot operators +------------- + +Nimrod offers a special family of dot operators that can be used to +intercept and rewrite proc call and field access attempts, referring +to previously undeclared symbol names. They can be used to provide a +fluent interface to objects lying outside the static confines of the +Nimrod's type system such as values from dynamic scripting languages +or dynamic file formats such as JSON or XML. + +When Nimrod encounters an expression that cannot be resolved by the +standard overload resolution rules, the current scope will be searched +for a dot operator that can be matched against a re-written form of +the expression, where the unknown field or proc name is converted to +an additional static string parameter: + +.. code-block:: nimrod + a.b # becomes `.`(a, "b") + a.b(c, d) # becomes `.`(a, "b", c, d) + +The matched dot operators can be symbols of any callable kind (procs, +templates and macros), depending on the desired effect: + +.. code-block:: nimrod + proc `.` (js: PJsonNode, field: string): JSON = js[field] + + var js = parseJson("{ x: 1, y: 2}") + echo js.x # outputs 1 + echo js.y # outputs 2 + +The following dot operators are available: + +operator `.` +------------ +This operator will be matched against both field accesses and method calls. + +operator `.()` +--------------- +This operator will be matched exclusively against method calls. It has higher +precedence than the `.` operator and this allows you to handle expressions like +`x.y` and `x.y()` differently if you are interfacing with a scripting language +for example. + +operator `.=` +------------- +This operator will be matched against assignments to missing fields. + +.. code-block:: nimrod + a.b = c # becomes `.=`(a, "b", c) + Term rewriting macros ===================== @@ -4758,42 +4811,6 @@ This may change in future versions of language, but for now use the ``finalizer`` parameter to ``new``. -delegator pragma ----------------- - -**Note**: The design of the delegator feature is subject to change. - -The delegator pragma can be used to intercept and rewrite proc call and field -access attempts referring to previously undeclared symbol names. It can be used -to provide a fluent interface to objects lying outside the static confines of -the Nimrod's type system such as values from dynamic scripting languages or -dynamic file formats such as JSON or XML. - -A delegator is a special form of the `()` operator marked with the delagator -pragma. When Nimrod encounters an expression that cannot be resolved by the -standard overload resolution, any delegators in the current scope will be -matched against a rewritten form of the expression following the standard -signature matching rules. In the rewritten expression, the name of the unknown -proc or field name is inserted as an additional static string parameter always -appearing in the leading position: - -.. code-block:: nimrod - a.b => delegator("b", a) - a.b(c, d) => delegator("b", a, c) - a b, c, d => delegator("a", b, c, d) - - -The delegators can be any callable symbol type (procs, templates, macros) -depending on the desired effect: - -.. code-block:: nimrod - proc `()` (field: string, js: PJsonNode): JSON {.delegator.} = js[field] - - var js = parseJson("{ x: 1, y: 2}") - echo js.x # outputs 1 - echo js.y # outputs 2 - - procvar pragma -------------- The `procvar`:idx: pragma is used to mark a proc that it can be passed to a |