Skip to content

Commit ef308e7

Browse files
committed
note how to do parameter checking
1 parent 27f19d3 commit ef308e7

11 files changed

Lines changed: 154 additions & 55 deletions

File tree

docs/extending/developing-code/extending/case-studies/selecting-builtin.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ In a cloned git directory, you can find some of missing using ``git grep -n '^#
3030
Below, we have a few examples of some actual Mathics3 Builtins that we have added. Hopefully you can use this as an aid for filling in one of the many Builtin Functions, such as one of the above.
3131

3232

33-
.. toctree::
34-
:maxdepth: 1
35-
36-
Undefined/index
37-
KroneckerProduct/index
38-
Curl/index
33+
* :ref: `Undefined`
34+
* :ref: `KroneckerProduct`
35+
* :ref: `Curl`
3936

4037

4138
As we added these Builtins, we recorded the steps that were taken. We ordered the list above to go from the simple to more advanced.

docs/extending/developing-code/extending/tutorial.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ After reading this you may want to go through the detailed examples in :ref:`Cas
1313

1414
tutorial/0-predefined
1515
tutorial/1-builtin
16-
tutorial/2-help-markup
17-
tutorial/3-test-markup
18-
tutorial/4-patterns
19-
tutorial/5-rules
20-
tutorial/6-attributes
21-
tutorial/7-warnings
16+
tutorial/2-parameter-checking
17+
tutorial/3-help-markup
18+
tutorial/4-test-markup
19+
tutorial/5-patterns
20+
tutorial/6-rules
21+
tutorial/7-attributes
22+
tutorial/8-warnings
2223

2324
.. TODO: Document Operator and SympyFunction
2425
.. TODO: Document interrupts

docs/extending/developing-code/extending/tutorial/1-builtin.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The docstring for the ``eval`` method is a Mathics3 pattern:
6262
6363
"Hello[person_String]"
6464
65-
Inside Mathic3 you can test what this matches using the Mathics3 ``MatchQ[]`` function:
65+
Inside Mathics3, you can test what this matches using the Mathics3 ``MatchQ[]`` function:
6666

6767
.. code-block:: mathematica
6868
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Checking Parameter Counts
2+
-------------------------
3+
4+
.. index:: parameter count checking
5+
6+
In a programming language that allows for generic and extensible
7+
arguments, what should we do when a function parameter list does not
8+
match any of the patterns of a particular builtin function specification?
9+
10+
In WMA and systems with rewrite rules, often what is done is the
11+
expression is returned unchanged. This allows users to write rewrite rules
12+
involving patterns that are not covered by your code.
13+
14+
For example:
15+
16+
.. code-block:: mathematica
17+
18+
In[1]:= Hello[] = "Hi, Rocky!";
19+
20+
In[2]= Hello[]
21+
Out[2]= "Hi, Rocky!"
22+
23+
Above, in essence we've added form of *Hello* that does not need a parameter.
24+
Instead, it provides a default value when none is given.
25+
26+
Pretty convenient and slick huh? Yes, but it can also lead to
27+
confusion in trying to find out where a problem is coming from since
28+
the definition can be spread to from many sources of definitions and
29+
rewrite rules which might be mentioned or part of other definitions:
30+
31+
.. code-block:: mathematica
32+
33+
In[3]:= Rocky[Hello[]] = "Hi again, Rocky";
34+
35+
In[3]= Rocky[Hello[]]
36+
Out[3]= "Hi again, Rocky"
37+
38+
Notice that the *In[3]* assignment takes place before the *In[1]*
39+
assignment. Here, these rewrite rules are in close proximity, but
40+
they could be spread out over time or over different files.
41+
42+
To help you figure how what evaluation is rewriting, see *TraceEvaluation*.
43+
44+
Many times though, at least for me and other simple-minded users, when
45+
I type a builtin-function with the wrong number of parameters, it is
46+
helpful to get an error message rather than the expression unchanged.
47+
48+
Generally, I do not expecting (or hope not to expect) some other clever rewrite rule,
49+
doing strange things.
50+
51+
So, it is sometimes helpful when writing a builtin function,
52+
to specify the number of parameters that are allowed, and give a standard
53+
message when the parameter counts do not match. This can be done setting class variables
54+
``eval_error`` and ``expected_args`` of a builtin function derived from ``Builtin``.
55+
56+
Here is an example:
57+
58+
.. code-block:: python
59+
60+
class Hello(Builtin):
61+
62+
# Set checking that the number of arguments required to exactly one.
63+
eval_error = Builtin.generic_argument_error
64+
expected_args = 1
65+
66+
67+
Now when I call *Hello* without any arguments you will see::
68+
69+
In[4]:= Hello[]
70+
Hello::argx: Hello called with 0 arguments; 1 argument is expected.
71+
72+
The value of ``expected_args`` does not have to be an integer. It can be a range, like:
73+
74+
* ``range(5)`` one to four arguments
75+
* ``range(2,4)``: two or three arguments
76+
77+
or a set:
78+
79+
* ``{1, 3, 5}`` one, three, or five arguments

docs/extending/developing-code/extending/tutorial/2-help-markup.rst renamed to docs/extending/developing-code/extending/tutorial/3-help-markup.rst

File renamed without changes.

docs/extending/developing-code/extending/tutorial/3-test-markup.rst renamed to docs/extending/developing-code/extending/tutorial/4-test-markup.rst

File renamed without changes.

docs/extending/developing-code/extending/tutorial/4-patterns.rst renamed to docs/extending/developing-code/extending/tutorial/5-patterns.rst

File renamed without changes.

docs/extending/developing-code/extending/tutorial/5-rules.rst renamed to docs/extending/developing-code/extending/tutorial/6-rules.rst

File renamed without changes.

docs/extending/developing-code/extending/tutorial/6-attributes.rst renamed to docs/extending/developing-code/extending/tutorial/7-attributes.rst

File renamed without changes.

docs/extending/developing-code/extending/tutorial/7-warnings.rst

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)