Methods and user-defined operators

@name[parameters]
body

@name[parameters][local;variables]
body

@static:name[parameters]
   [3.4.1]
body of class' method which can be only called statically (
more details)

@name[*parameters]
   [3.4.1]
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.

Example:

@main[] 
^call[a;b;c] 

@call[p;*args][k;v] 
p=$p 
^args.foreach[k;v]{ 
   $k=$v 
}[^#0A] 


Outputs:
p=a 
0=b 
1=c 



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.

Example:

@main[]  
$a[
Static field ^$a of class MAIN]
^test[
Method's parameter]

@test[a]  
^$a
 - $a  <br />  
^$self.a
 - $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.

Example:
@main[]
$a(2)
$b(3)
$summa[^sum[$a;$b]]
$summa

@sum[a;b]
^eval($a+$b)
$result[
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]
If
result variable is explicitly declared, this means to Parser that it should ignore all whitespace characters in method code (in the curly brackets).

Example:
@lookup[table;findcol;findvalue][result]
^if
(^table.locate[$findcol;$findvalue]){
   $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 $caller.method.name   [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:

@steppedfor[name;from;to;step;code]
$caller.$name($from)
^
while($caller.$name<=$to){
   $code
   ^caller.$name.
inc($step)
}

Now the call…


@somewhere[][i]
^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]
If
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 | http://www.artlebedev.com Last updated: 20.04.2017