Undefined values: Difference between revisions

m
→‎{{header|Raku}}: Fix links and comments: Perl 6 --> Raku
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Raku}}: Fix links and comments: Perl 6 --> Raku)
Line 1,212:
=={{header|Raku}}==
(formerly Perl 6)
Perl 6Raku has "interesting" values of undef, but unlike Perl 5, doesn't actually have a value named <tt>undef</tt>. Instead, several very different meanings of undefinedness are distinguished. First, <tt>Nil</tt> represents the absence of a value. The absence of a value cannot be stored. Instead, an attempt to assign <tt>Nil</tt> to a storage location causes that location to revert to its uninitialized state, however that is defined.
 
<lang perl6>my $x; $x = 42; $x = Nil; say $x.WHAT; # prints Any()</lang>
 
This <tt>Any</tt> is an example of another kind of undefined type, which is a typed undef. All reference types have an undefined value representing the type. You can think of it as a sort of "type gluon" that carries a type charge without being a "real" particle. Hence there are undefined values whose names represent types, such as <tt>Int</tt>, <tt>Num</tt>, <tt>Str</tt>, and all the other object types in Perl 6Raku. As generic objects, such undefined values carry the same metaobject pointer that a real object of the type would have, without being instantiated as a real object. As such, these types are in the type hierarchy. For example, <tt>Int</tt> derives from <tt>Cool</tt>, <tt>Cool</tt> derives from <tt>Any</tt>, and <tt>Any</tt> derives from <tt>Mu</tt>, the most general undefined object (akin to Object in other languages). Since they are real objects of the type, even if undefined, they can be used in reasoning about the type. You don't have to instantiate a <tt>Method</tt> object in order to ask if <tt>Method</tt> is derived from <tt>Routine</tt>, for instance.
 
<lang perl6>say Method ~~ Routine; # Bool::True</lang>
Line 1,225:
my Str $z; say $z.WHAT; # Str()</lang>
 
The user-interface for definedness are [http://design.perl6raku.org/S12.html#Abstract_vs_Concrete_types type smilies] and the <tt>with</tt>-statement.
 
<lang perl6>my Int:D $i = 1; # if $i has to be defined you must provide a default value
Line 1,235:
</lang>
 
There are further some [http://design.perl6raku.org/S03.html operators] for your convenience.
 
<lang perl6>my $is-defined = 1;
Line 1,255:
dd $s; # this be Bool::False</lang>
 
Finally, another major group of undefined values represents failures. Perl 6Raku is designed with the notion that throwing exceptions is bad for parallel processing, so exceptions are thrown lazily; that is, they tend to be returned in-band instead as data, and are only thrown for real if not properly handled as exception data. (In which case, the exception is required to report the original difficulty accurately.) In order not to disrupt control flow and the synchronicity of event ordering, such failures are returned in-band as a normal values. And in order not to subvert the type system when it comes to return types, failure may be mixed into any reference type which produces an undefined, typed value which is also indicates the nature of the failure.
 
Native storage types work under different rules, since most native types cannot represent failure, or even undefinedness. Any attempt to assign a value to a storage location that cannot represent a failure will cause an exception to be thrown.
2,392

edits