the delegator pragma becomes a set of dot operators
This commit is contained in:
parent
a158053ae9
commit
492fa86638
7 changed files with 169 additions and 75 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue