is. Checking type

object is type

The operator checks if left operand is an object of specified type and returns bool value (true/false). It is handy to use the operator in cases when a variable may contain a single value or a set of values (hash), as well as to check if a method is defined.

type-name of type. It may be a system name (hash, junction, …), or name of user-defined class.

Simple type check
@main[]
$date[
1999-10-10]
#$date[^date::now[]]
^if($date is string){
    ^parse[$date]
}{
    ^print_date[$date.
year;$date.month;$date.day]
}

@parse[date_string][date_parts]
$date_parts[^date_string.
match[(\d{4})-(\d{2})-(\d{2})][]]
^print_date[$date_parts.
1;$date_parts.2;$date_parts.3]

@print_date[year;month;day]
Working with date:<br />
Day  $day
<br />
Month$month
<br />
Year:  $year
<br />

This example will check the type of variable
$date and will either perform syntactical analysis or pass to method print_date the fields of $date (if type is object of class date).

Checking if method is defined
The value of $method_name is also junction, that is why we should also use is and not def in this case.
@body[]
body

@
main[]
Start
^if($body is 
junction){
   ^body[]
}{
   Method "body" is not defined!
}
Finish

Note: using operator is you can't check variables which contains code because of any address to such variables execute the code.
For such check ^reflection:is[] should be used.


Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 21.07.2016