Methods and user-defined operators



body of class' method which can be only called statically (
more details)

method's body which can accept valiable number of parameters

@name[param1;param2;*parameters]   [3.4.1]
method's body which can accept variable number of parameters

Method is a code block, which has name, accepts parameters, and returns result. Names of a method's parameters are separated by semicolon. Method can also have local variables, which should be declared in method's header after declaration of parameters. Names of local variables are also separated by semicolon.

Local variables are visible only within the operator or method they belong to and from within the operators or methods they refer to (cf.
$caller described further in the text).

While defining a method, you can use not only parameters and local variables but also any other names, thus working with fields of a class or object. This will depend on how you called the method statically, or dynamically.

In Parser, you can extend core set of operators, since methods of class MAIN are considered operators.
Important notice: operators are methods of class MAIN, but in contrast to other classes' methods, you can call them from any other class by using their name only, i.e. instead of using sophisticated
^MAIN:include[…], you can use just ^include[…].

In the methods which can accept variable number of parameters (such @name[*parameters]), all "excessive" parameters are available as a hash with numeric keys. Key 0 corresponds to the first "excessive" parameter.





System variable: self
All methods and operators have a local variable self. It contains reference to the current object; in static methods, its content is the same is that of $CLASS.


Static field ^$a of class MAIN]
Method's parameter]

 - $a  <br />  
 - $self.a  
The code will output:

$a - Method's parameter 
$self.a - Static field $a of class MAIN

System variable: result
All methods and operators have a local variable result. If any value is assigned to it, it will be considered the result of the method's work. The value of result can be read and used in calculations.


I won't say anything!]

In this case, the client will receive a string I won't say anything!, but not the result of addition of the two numbers.

System variable: result, explicit declaration   [3.1.5]   [3.4.5]
result variable is explicitly declared, this means to Parser that it should ignore all whitespace characters in method code (in the curly brackets).

   $yes[yes found] $yes
not found

In this case, the client will receive either the 'yes found' value or 'notfound' value.
What important is there would be no whitespace characters written in the method code returned (no line breaks, tabs or spaces).
Important: before version 3.4.5, trying to write
not found text directly in the body of the method will result in an error.

System variable: caller
All methods and operators have local variable caller, which stores the method's or operator's "scope of the call".

You can use it:
·refer to-$caller.variable_name_to_refer-or assign $caller.variable_name_to_assign[value]-a variable as if you were in the place where the defined method or operator was called from;  
·to find out who called the method or operator. In this case you will need to use $caller.self or $caller.method   [3.4.5];  
·to find out the caller method name you will need to use $   [3.4.5].  

For example, you need an operator which would be like system
for, yet somewhat different from it. You can create it by yourself, using an opportunity to change local variable with name sent to you within the scope of the call of your operator:


Now the call…

^steppedfor[i](1;10;2){$i }

…will output "
1 3 5 7 9 ". Note: it is the local variable of method somewhere that is changed.

Notice: You may need the opportunity to find out the scope of the call to specify the scope of code's compilation (cf. "Process. Compiling and processing string".

System variable: locals, explicit declaration   [3.3.0]
locals variable is explicitly declared, this means to Parser that all variables used in the method are declarated locally.
To access object or class variables you should use self or CLASS prefixes.

Copyright © 1997–2021 Art. Lebedev Studio | Last updated: 20.04.2017