Core Function isVarClass
From Sputnik Wiki
(Difference between revisions)
(→Example) |
|||
| Line 63: | Line 63: | ||
println( isVarClass($cat1, "vec3") ? "TRUE" : "FALSE" ); | println( isVarClass($cat1, "vec3") ? "TRUE" : "FALSE" ); | ||
println( isVarClass($moo, "vec3") ? "TRUE" : "FALSE" ); | println( isVarClass($moo, "vec3") ? "TRUE" : "FALSE" ); | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | Alternative way to see if a variable contains a class object by name | ||
| + | |||
| + | <syntaxhighlight lang="sputnik"> | ||
| + | Class Vec3 | ||
| + | { | ||
| + | my $x = 0; | ||
| + | my $y = 0; | ||
| + | my $z = 0; | ||
| + | Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) | ||
| + | { | ||
| + | $this->$x = (double)$x1; | ||
| + | $this->$y = (double)$y1; | ||
| + | $this->$z = (double)$z1; | ||
| + | } | ||
| + | }; | ||
| + | $cat1 = new Vec3(10, 20, 30); | ||
| + | println( $cat1 ~~ Vec3 ? "TRUE" : "FALSE" ); | ||
| + | println( $moo ~~ Vec3 ? "TRUE" : "FALSE" ); | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | Yet another alternative way to see if a variable contains a class object by name | ||
| + | |||
| + | <syntaxhighlight lang="sputnik"> | ||
| + | Class Vec3 | ||
| + | { | ||
| + | my $x = 0; | ||
| + | my $y = 0; | ||
| + | my $z = 0; | ||
| + | Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) | ||
| + | { | ||
| + | $this->$x = (double)$x1; | ||
| + | $this->$y = (double)$y1; | ||
| + | $this->$z = (double)$z1; | ||
| + | } | ||
| + | }; | ||
| + | $cat1 = new Vec3(10, 20, 30); | ||
| + | println( $cat1 is Vec3 ? "TRUE" : "FALSE" ); | ||
| + | println( $moo is Vec3 ? "TRUE" : "FALSE" ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] | ||
Revision as of 14:26, 21 August 2013
isVarClass( $variable, <type> )
Contents |
Description
Check if a variable's object type is a class.
Parameters
variable
The variable to check.
Return Value
Success: Returns 1.
Failure: Returns 0 if parameter is not an array variable.
Remarks
Can be useful to validate array/non-array parameters to user-defined functions.
Example
A simple example to see if a variable is a class variable :
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( isVarClass($cat1) ? "TRUE" : "FALSE" ); println( isVarClass($moo) ? "TRUE" : "FALSE" );
Same example but this time making sure the class is a vec3 and nothing else:
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( isVarClass($cat1, "vec3") ? "TRUE" : "FALSE" ); println( isVarClass($moo, "vec3") ? "TRUE" : "FALSE" );
Alternative way to see if a variable contains a class object by name
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( $cat1 ~~ Vec3 ? "TRUE" : "FALSE" ); println( $moo ~~ Vec3 ? "TRUE" : "FALSE" );
Yet another alternative way to see if a variable contains a class object by name
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( $cat1 is Vec3 ? "TRUE" : "FALSE" ); println( $moo is Vec3 ? "TRUE" : "FALSE" );