You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
== instanceof ==
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Checks to see if the value is, extends, or implements the given type. Keyword usage is preferred:
=== Vital Info ===
{| style="width: 40%;" cellspacing="1" cellpadding="1" border="1" class="wikitable"
|-
! scope="col" width="20%" |
! scope="col" width="80%" |
|-
! scope="row" | Name
| instanceof
|-
! scope="row" | Returns
| boolean
|-
! scope="row" | Usages
| value, type
|-
! scope="row" | Throws
|
|-
! scope="row" | Since
| 3.3.1
|-
! scope="row" | Restricted
| @value instanceof int
. The opposite operation is @value notinstanceof int
.No
|-
! scope="row" | Optimizations
| [[../../Optimizer#OPTIMIZE_DYNAMIC|OPTIMIZE_DYNAMIC]]
|}Null is a special value, while any type may be assigned null, it does not extend any type, and therefore "null instanceof AnyType" will always return false. Likewise, other than null, all values extend "mixed", and therefore "anyNonNullValue instanceof mixed" will always return true. There is no (single) functional equivalent to the notinstanceof keyword.
=== Usages ===
@value notinstanceof int
simply compiles to not(instanceof(@value, int)).instanceof(value, type)=== Examples === ====Example 1==== Basic usage Given the following code:
mixed @a = 5; // Actually an int
msg(@a instanceof int); // true
msg(@a instanceof string); // false

1 {{object|mixed}} @a = 5; // Actually an int
2
3 {{function|msg}}(@a {{keyword|instanceof}} {{object|int}}); // true
4
5 {{function|msg}}(@a {{keyword|instanceof}} {{object|string}}); // false
true false====Example 2==== Functional usage Given the following code:
instanceof(5, int)

1 {{keyword|instanceof}}(5, {{object|int}})
:true====Example 3==== Inverted usage Given the following code:
mixed @a = 5;
msg(@a notinstanceof int); // false
msg(@a notinstanceof string); // true

1 {{object|mixed}} @a = 5;
2 {{function|msg}}(@a {{keyword|notinstanceof}} {{object|int}}); // false
3
4 {{function|msg}}(@a {{keyword|notinstanceof}} {{object|string}}); // true
false true====Example 4==== Inverted functional usage Given the following code:
!instanceof(5, int)

1 !{{keyword|instanceof}}(5, {{object|int}})
:false
Find a bug in this page? Edit this page yourself, then submit a pull request. (Note this page is automatically generated from the documentation in the source code.)