Type objects are always undefined, but I've seen some tests that use .defined
and some that use .DEFINITE
. Is there any case where those might be different? I tend to think that any method that's all uppercase isn't for everyday work and would prefer .defined
for this task.
my $class = IntStr;
put '-' x 20; # False
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # False
$class = Nil;
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # True
$class = '';
put $class.DEFINITE;
put $class.defined;
In the output I'm looking for any case where the answers to the two methods would be different:
--------------------
False
False
--------------------
False
False
--------------------
True
True
.DEFINITE
should be considered a Macro (just like .WHAT
, .HOW
etc). It is directly handled in the Actions and converted to a nqp::p6definite()
op.
.defined
is a method that lives in Mu
and which can be overridden by your class. It is in fact overridden for Failure
, so that an instantiated Failure
can act as a undefined value in e.g. an if
statement (and "handle" the Failure).
my $a = Failure.new("foo");
say "bar" if $a; # no output
say $a; # outputs "(HANDLED) foo", but no longer throws
So to answer your question:
my $a = Failure.new("foo");
say $a.DEFINITE; # True
say $a.defined; # False
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