Jump to content

Create an object/Native demonstration: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 324:
 
jq objects, however, are really just values: they are immutable, and cannot be "deleted" any more than the number 1 can be deleted.
 
 
 
 
 
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
 
fun main(args: Array<String>) {
// This line creates a read-only map which cannot be changed in any way nor cleared
val map = mapOf('A' to 65, 'B' to 66, 'C' to 67)
println(map)
}</lang>
 
{{out}}
<pre>
{A=65, B=66, C=67}
</pre>
 
=={{header|Julia}}==
Line 372 ⟶ 354:
</lang>
All tests pass.
 
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
 
fun main(args: Array<String>) {
// This line creates a read-only map which cannot be changed in any way nor cleared
val map = mapOf('A' to 65, 'B' to 66, 'C' to 67)
println(map)
}</lang>
 
{{out}}
<pre>
{A=65, B=66, C=67}
</pre>
 
=={{header|M2000 Interpreter}}==
Line 504 ⟶ 500:
val1: 1
val4 is empty</pre>
 
=={{header|Perl}}==
<lang perl>package LockedHash;
Line 583 ⟶ 580:
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|2016.08}}
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($key) ?? $!hash.AT-KEY($key) !! Failure.new(q{can't store value for unknown key});
}
method DELETE-KEY($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 (Any))
(1 42)
can't store value for unknown key
in block <unit> at native-demonstration.p6:17
 
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|Phix}}==
Line 920 ⟶ 863:
#fenced-hash(("b" . 66) ("a" . 55))
#fenced-hash(("b" . 66) ("a" . 1))</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.08}}
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($key) ?? $!hash.AT-KEY($key) !! Failure.new(q{can't store value for unknown key});
}
method DELETE-KEY($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 (Any))
(1 42)
can't store value for unknown key
in block <unit> at native-demonstration.p6:17
 
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|Ring}}==
Line 1,187 ⟶ 1,186:
end
end</lang>
 
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/OuVZ3bT/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/qW5qzmdKSZSyAbZEqDROoA Scastie (remote JVM)].
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.