In the Give
example on the previous pages, we talked about calls matching or not matching methods, but we didn't explain how matching worked. So let's say a little bit about that now.
A call matches a method if every part of the call — the task and each of the parameters — match the corresponding part of the method. So for example, the call:
[Give "Mary" "Jill" "a nice raise"]
Doesn't match the method:
Hello ?who: Hello, who!
Both because the tasks are different (Give
≠Hello
), and because they have different numbers of parameters (three in the call, one in the method).
Matching a single parameter
Parameters can either be specific values, like 7
or "a nice raise"
, or variables like ?giver
. In this code, the first two methods specify values for their parameters, and the last specifies a variable:
Reflexive “Mr. Boss”: himself
Reflexive “Mary”: herself
Reflexive ?who: themself
Similarly, a call can specify either a value or a variable for the parameter.
Matching values to values
When both the call and the method give specific values for a parameter, then they match only if they give the same value. So the call
[Reflexive "Mr. Boss"]
matches the first method above, but not the second.1 And the call [Reflexive "Mary"]
matches the second but not the first.
Matching variables to values
When one specifies a variable and the other a value, they match. However, the system connects that variable with that value: any subsequent uses of the variable will use that value instead. So the call [Reflexive "Mr. Boss"]
matches the third method above, but would connect the method's variable ?who
to the value "Mr. Boss"
.
Note that the variable doesn't have to appear on the method side. It can appear in the call instead. If we do the call [Reflexive ?x]
, it matches each of the above methods, but also connects ?x
to the parameter in the chosen method. That gives programmers a way not only of calling a task with a parameter that means “I don't care which”, but also find out what value was chosen.
Matching variables to variables
Finally, it's possible for both the caller and the method to list a variable for the parameter, for example when we match the call [Reflexive ?x]
to the method:
Reflexive ?who: themself
When we do this, the match succeeds. But rather than connecting the variables to a value, we connect the variables to each other. Then, should we connect either to something else in the future, we will be connecting both.
Notes
-
It also matches the third, but we'll talk about variables shortly.↩