Undefined values: Difference between revisions

m (→‎{{header|REXX}}: used gooder Unglish, added/changed whitespace and comments.)
Line 809:
my Int $y; say $y.WHAT; # Int()
my Str $z; say $z.WHAT; # Str()</lang>
 
The user-interface for definedness are type smilies and the with-statement.
 
<lang perl6>my Int:D $i = 1; # if $i has to be defined you must provide a default value
multi sub foo(Int:D $i where * != 0){ (0..100).roll / $i } # we will never divide by 0
multi sub foo(Int:U $i){ die 'WELP! $i is undefined' } # because undefinedness is deadly
multi sub foo(Int:_ where * == 0){ die q{I'm sorry, Dave, I'm afraid I can't do that.} }
 
with $i { say 'defined' } # as "if" is looking for Bool::True, "with" is looking for *.defined
with 0 { say '0 may not divide but it is defined' }
</lang>
 
Finally, another major group of undefined values represents failures. Perl 6 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.
Anonymous user