I intentionally avoid the term defined
because a variable may very well have a defined value but the .defined
method will return false (Failures, for instance).
Is there any way to determine whether a variable has had a value set to it?
my $foo;
say $foo; # (Any), its type object, no value assigned
my Str $bar;
say $bar; # (Str), its type object, no value assigned
my $abc = Str;
say $abc; # (Str), the type object we assigned to $abc
How can we disinguish $bar
(no value set, typed as Str
) from $abc
(value set to Str
)?
Given that $bar.WHICH == $abc.WHICH
, but $bar.VAR.WHICH !== $abc.VAR.WHICH
, and methods like .defined
will return false for each, is there any quick and easy way to determine that there is a set value?
I supposed it could be checked against the default value, but then there'd be no way to distinguish between the value being by virtue of unset, versus by being set in code.
You might try to assign a default value to a variable, instead of keeping it undefined:
my Str $bar is default("");
$bar
will be Str
only if it's assigned that value type; if its value is deleted via assigning Nil
it will default again to the empty string. As a matter of fact, the default for a variable is its type object, so:
my Str $foo;
my $bar = Str;
say $foo eqv $bar
will, in fact, return True.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With