Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Perl 6's DEFINITE and defined methods?

Tags:

raku

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
like image 686
brian d foy Avatar asked Feb 26 '18 10:02

brian d foy


1 Answers

.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
like image 129
Elizabeth Mattijsen Avatar answered Sep 28 '22 10:09

Elizabeth Mattijsen