Create an object/Native demonstration: Difference between revisions

Line 322:
Actually thrown at:
in block <unit> at native-demonstration.p6:17</pre>
 
By defining [http://design.perl6.org/S12.html#FALLBACK_methods FALLBACK] any class can handle undefined method calls. Since any class inherits plenty of methods from <tt>Any</tt> our magic object will be more of a novice conjurer then a master wizard proper.
 
<lang perl6>class Magic {
has %.hash;
multi method FALLBACK($name, |c) is rw { # this will eat any extra parameters
%.hash{$name}
}
 
multi method FALLBACK($name) is rw {
%.hash{$name}
}
}
 
my $magic = Magic.new;
$magic.foo = 10;
say $magic.foo;
$magic.defined = False; # error</lang>
 
{{output}}
<pre>10
Cannot modify an immutable Bool
in block <unit> at native-demonstration.p6:15<pre>
 
=={{header|Python}}==
Anonymous user