Create an object/Native demonstration: Difference between revisions

(Rewritten D entry, fits better the Task)
Line 285:
LockedHash::STORE('LockedHash=HASH(0x8cebe14)', 'x', 1) called at test.pl line 66
eval {...} called at test.pl line 66</lang>
=={{header|Perl 6}}==
{{works with|rakudo|2013-02-22}}
Here we use delegation to handle all the normal hash methods that we don't need to override to define our new class.
<lang perl6>class FixedHash {
has $.hash handles *;
method new(*@args) { self.bless: *, hash => Hash.new: @args }
method at_key(FixedHash:D: $key is copy) is rw {
$!hash.exists($key) ?? $!hash.at_key($key) !! Nil;
}
method delete($key) { $!hash.{$key} = Nil }
}
 
# Testing
my $fh = FixedHash.new: "a" => 1, "b" => 2;
say $fh<a b>; # 1 2
$fh<b>:delete;
say $fh<a b>; # 1 Nil
$fh<b> = 42;
say $fh<a b>; # 1 42
say $fh<c>; # Nil
$fh<c> = 43; # error</lang>
{{out}}
<pre>1 2
1 Nil
1 42
Nil
Cannot assign to a non-container
in block at freezehash:21</pre>
 
=={{header|Python}}==
Anonymous user