Create an object/Native demonstration: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Kotlin)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(35 intermediate revisions by 14 users not shown)
Line 1:
{{draft task}}
 
Create a Hash/Associative Array/Dictionary-like object, that can be initialized with some default key/value pairs using the languages native method of object creation. The object should behave like a native Hash/Associative Array/Dictionary of the language, if any, but with the following differences:
 
* No new itemkeys can be added;
* Keys cannot be removed;
* Item cannot be deleted, (but native delete method may used to reset the item's value to default) ;
* Attempting to delete a key should set that keys value back to that used during initialisation.
(The value assigned to keys may be changed by normal assignment however).
 
If the language supports '''Magic Methods''', then show how these work.
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">map mapa
mapa["A"] = 65
mapa["B"] = 66
mapa["C"] = 67
 
foreach valor in mapa
print valor
print mapa[valor]
next valor</syntaxhighlight>
{{out}}
<pre>A
65
B
66
C
67</pre>
 
==={{header|FreeBASIC}}===
FB doesn't have Dict natively, but we can implement them via Type
<syntaxhighlight lang="freebasic">Type dict
m1 As String*1
m2 As Integer
End Type
 
Dim mapOf(1 To 3) As dict => {("A", 65), ("B", 66), ("C", 67)}
 
For i As Integer = 1 To Ubound(mapOf)
Print mapOf(i).m1
Print mapOf(i).m2
Next i</syntaxhighlight>
{{out}}
<pre>A
65
B
66
C
67</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <utility>
 
using namespace std;
 
template<typename T>
class FixedMap : private T
{
// Two standard maps are used to implement FixedMap. One as a private
// base class which will allow the values (but not the keys) to be modified.
// Members of a private base class are not exposed to the derived class which will
// prevent keys from being added or deleted. Another map will hold copies of
// the initial values.
T m_defaultValues;
public:
FixedMap(T map)
: T(map), m_defaultValues(move(map)){}
// Expose members of the base class that do not modify the map.
using T::cbegin;
using T::cend;
using T::empty;
using T::find;
using T::size;
 
// Also expose members that can modify values but not add or remove keys.
using T::at;
using T::begin;
using T::end;
// The [] operator will normally add a new key if the key is not already in the
// map. Instead, throw an error if the key is missing.
auto& operator[](typename T::key_type&& key)
{
// Make it behave like at()
return this->at(forward<typename T::key_type>(key));
}
// Instead of removing a key, change the sematics of erase() to restore
// the original value of the key.
void erase(typename T::key_type&& key)
{
T::operator[](key) = m_defaultValues.at(key);
}
 
// Also change the sematics of clear() to restore all keys
void clear()
{
// Reset the base class using the defaults
T::operator=(m_defaultValues);
}
};
 
// Print the contents of a map
auto PrintMap = [](const auto &map)
{
for(auto &[key, value] : map)
{
cout << "{" << key << " : " << value << "} ";
}
cout << "\n\n";
};
 
int main(void)
{
// Create a fixed map based on the standard map
cout << "Map intialized with values\n";
FixedMap<map<string, int>> fixedMap ({
{"a", 1},
{"b", 2}});
PrintMap(fixedMap);
cout << "Change the values of the keys\n";
fixedMap["a"] = 55;
fixedMap["b"] = 56;
PrintMap(fixedMap);
cout << "Reset the 'a' key\n";
fixedMap.erase("a");
PrintMap(fixedMap);
cout << "Change the values the again\n";
fixedMap["a"] = 88;
fixedMap["b"] = 99;
PrintMap(fixedMap);
cout << "Reset all keys\n";
fixedMap.clear();
PrintMap(fixedMap);
try
{
// Adding or retrieving a missing key is a run time error
cout << "Try to add a new key\n";
fixedMap["newKey"] = 99;
}
catch (exception &ex)
{
cout << "error: " << ex.what();
}
}
</syntaxhighlight>
{{out}}
<pre>Map intialized with values
{a : 1} {b : 2}
 
Change the values of the keys
{a : 55} {b : 56}
 
Reset the 'a' key
{a : 1} {b : 56}
 
Change the values the again
{a : 88} {b : 99}
 
Reset all keys
{a : 1} {b : 2}
 
Try to add a new key
error: map::at</pre>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">struct DefaultAA(TK, TV) {
TV[TK] standard, current;
 
Line 42 ⟶ 209:
d.remove("a");
d.writeln; // ["a":1, "b":66]
}</langsyntaxhighlight>
{{out}}
<pre>["a":1, "b":2]
Line 49 ⟶ 216:
55
["a":1, "b":66]</pre>
 
=={{header|Go}}==
Go's built-in map type is mutable and so, to complete this task, we need to create a read-only wrapper for it which doesn't permit further items to be added or existing items to be deleted though does allow them to be reset to their default value.
 
First create a sub-directory, romap, of the project directory and place the following package in it:
<syntaxhighlight lang="go">package romap
 
type Romap struct{ imap map[byte]int }
 
// Create new read-only wrapper for the given map.
func New(m map[byte]int) *Romap {
if m == nil {
return nil
}
return &Romap{m}
}
 
// Retrieve value for a given key, if it exists.
func (rom *Romap) Get(key byte) (int, bool) {
i, ok := rom.imap[key]
return i, ok
}
 
// Reset value for a given key, if it exists.
func (rom *Romap) Reset(key byte) {
_, ok := rom.imap[key]
if ok {
rom.imap[key] = 0 // default value of int
}
}</syntaxhighlight>
 
This package can now be imported and used within the main package as follows:
<syntaxhighlight lang="go">package main
 
import (
"./romap"
"fmt"
)
 
func main() {
// create a normal map
m := map[byte]int{'A': 65, 'B': 66, 'C': 67}
 
// place it in a read-only wrapper so no new item can be added or item deleted.
rom := romap.New(m)
 
// retrieve value represented by 'C' say
i, _ := rom.Get('C')
fmt.Println("'C' maps to", i)
 
// reset this to default value (doesn't actually delete the key)
rom.Reset('C')
i, _ = rom.Get('C')
fmt.Println("'C' now maps to", i)
}</syntaxhighlight>
 
{{out}}
<pre>
'C' maps to 67
'C' now maps to 0
</pre>
 
=={{header|J}}==
Line 54 ⟶ 282:
Given a list of keys and an associated list of values, the idiomatic way of expressing this concept in J would be:
 
<langsyntaxhighlight lang="j">lookup=: values {~ keys&i.</langsyntaxhighlight>
 
For example:
 
<langsyntaxhighlight lang="j"> lookup=: 10 20 30 40 50 {~ (;:'this is a test')&i.
lookup ;:'a test'
30 40</langsyntaxhighlight>
 
Notes:
Line 69 ⟶ 297:
 
3) In J, objects are always referenced, but all data is passed by value. This means that objects can never be passed to a function -- only a reference to an object (its name) can be passed. This means that objects exist only in the way things are named, in J. So for the most part, we do not call things "objects" in J, and this task has nothing to do with what are called "objects" in J. However, this does demonstrate how things are created in J -- you write their definition, and can use them and/or assign to names or inspect them or whatever else.
 
=={{header|Java}}==
 
Java supports unmodifiable maps, sets, lists, and other more specialized unmodifiable collections. In this example, we have a unmodifiable map. We first create an ordinary map, modify as needed, then call the <code>Collections.unmodifiableMap</code>. We can subsequently read the map, but modification is not permitted. The returned map will subsequently throw a <code>UnsupportedOperationException</code> exception if a mutation operator is called. Several are demonstrated below.
 
<syntaxhighlight lang="java">
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
// Title: Create an object/Native demonstration
 
public class ImmutableMap {
 
public static void main(String[] args) {
Map<String,Integer> hashMap = getImmutableMap();
try {
hashMap.put("Test", 23);
}
catch (UnsupportedOperationException e) {
System.out.println("ERROR: Unable to put new value.");
}
try {
hashMap.clear();
}
catch (UnsupportedOperationException e) {
System.out.println("ERROR: Unable to clear map.");
}
try {
hashMap.putIfAbsent("Test", 23);
}
catch (UnsupportedOperationException e) {
System.out.println("ERROR: Unable to put if absent.");
}
for ( String key : hashMap.keySet() ) {
System.out.printf("key = %s, value = %s%n", key, hashMap.get(key));
}
}
private static Map<String,Integer> getImmutableMap() {
Map<String,Integer> hashMap = new HashMap<>();
hashMap.put("Key 1", 34);
hashMap.put("Key 2", 105);
hashMap.put("Key 3", 144);
 
return Collections.unmodifiableMap(hashMap);
}
}
</syntaxhighlight>
 
{out}}
<pre>
ERROR: Unable to put new value.
ERROR: Unable to clear map.
ERROR: Unable to put if absent.
key = Key 1, value = 34
key = Key 2, value = 105
key = Key 3, value = 144
</pre>
 
=={{header|JavaScript}}==
Line 75 ⟶ 364:
{{works with|JavaScript|1.7}}
 
<langsyntaxhighlight lang="javascript">var keyError = new Error("Invalid Key Error (FixedKeyDict)") ;
 
function FixedKeyDict(obj)
Line 137 ⟶ 426:
return "FixedKeyDict{" + s + "}" ;
} ;
}</langsyntaxhighlight>
 
Test run:
 
<langsyntaxhighlight lang="javascript">
const BR = "<BR>\n"
 
Line 175 ⟶ 464:
pl("error test : " + e.message) ;
}
</syntaxhighlight>
</lang>
 
output :
Line 198 ⟶ 487:
 
=={{header|jq}}==
jq objects are JSON objects and can be created using JSON syntax, e.g. <langsyntaxhighlight lang="jq">{"language": "jq"}</langsyntaxhighlight>
Objects can also be created programmatically, e.g. <langsyntaxhighlight lang="jq">{"one": 1} + {"two": 2}</langsyntaxhighlight>
 
jq objects, however, are really just values: they are immutable, and cannot be "deleted" any more than the number 1 can be deleted.
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
using BackedUpImmutable
 
function testBackedUpImmutableDict()
fibr = BackedUpImmutableDict{String,Int64}(["a" => 0, "b" => 1, "c" => 1, "d" => 2,
"e" => 3, "f" => 5, "g" => 8, "h" => 13, "i" => 21, "j" => 34, "extra" => -1])
 
x = fibr["extra"]
@test x == -1
fibr["extra"] = 0
y = fibr["extra"]
@test y == 0
restore!(fibr, "extra")
z = fibr["extra"]
@test z == -1
@test_throws String begin fibr["k"] = 55 end
fibr["a"] = 9
fibr["b"] = 7
# test restore all to default
restoreall!(fibr)
@test fibr["a"] == 0
end
</syntaxhighlight>
All tests pass.
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(args: Array<String>) {
Line 210 ⟶ 529:
val map = mapOf('A' to 65, 'B' to 66, 'C' to 67)
println(map)
}</langsyntaxhighlight>
 
{{out}}
Line 216 ⟶ 535:
{A=65, B=66, C=67}
</pre>
 
=={{header|M2000 Interpreter}}==
{{trans|C sharp}}
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Class LockedHash {
Private:
inventory Vars ' no same keys
unlock
module nosuchvariable {
Error "No such value:"+letter$
}
module NoNewItem {
Error "No new item, use unlock method before"
}
module NoRemoveItem {
Error "Can't remove item, use unlock method before"
}
Public:
module Unlock {
.unlock<=True
}
module Writeln {
m=each(.Vars)
while m {
Print Quote$(Eval$(m, m^));",";Eval(m),
}
Print
}
Value (st$){
st$=Ucase$(st$)
if exist(.Vars, st$) then =Eval(.Vars) : Exit
.nosuchvariable st$
}
Set (st$){
st$=Ucase$(st$)
Read val
if exist(.Vars, st$) then Return .Vars, st$:=val : Exit
If .unlock then { Append .Vars, st$:=val} Else .NoNewItem
}
module Remove (st$) {
if not .unlock then .NoRemoveItem
st$=Ucase$(st$)
Try {
delete .Vars, st$
}
}
module Clear {
Clear .Vars
}
Class: ' this part exist only at construction
module LockedHash {
While match("SN") {
read st$, val
st$=ucase$(st$)
\\ if we append key which exist we get error
Append .Vars, st$:=val
}
}
}
d=LockedHash("a", 1, "b", 2)
d.writeln
d("a")=55 : d("b")=66
d.writeln
d.clear
d.writeln
d.unlock
d("a")=55 : d("b")=66
Print d("a")=55, d("a")/d("b")<1
d.remove "a"
d.writeln
}
Checkit
</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a[1] = "Do not modify after creation";
a[2] = "Native demonstration";
Protect[a];</langsyntaxhighlight>
Example usage:
<pre>a[3] = 2
->Set::write: Tag a in a[1] is Protected. >>
</pre>
 
=={{header|Nim}}==
We leverage native stdlib table as our own object by implementing limited actual native table functionalities.
<syntaxhighlight lang="nim">import tables, options
 
type
MyTable = object
table: TableRef[string, int]
 
# return empty if the key is not available
proc `[]`(m: MyTable, key: string): Option[int] =
if key in m.table: result = some m.table[key]
else: result = none int
 
# update an item, doing nothing if the key is available during first initialization
proc `[]=`(m: var MyTable, key: string, val: int) =
if key notin m.table: return
m.table[key] = val
 
proc reset(m: var MyTable) =
for _, v in m.table.mpairs: v = 0
 
# sugar for defining MyTable object
proc toTable(vals: openarray[(string, int)]): MyTable =
result.table = newTable vals
 
proc main =
# MyTable construction
var myobj = {"key1": 1, "key2": 2, "key3": 3}.toTable
# test getting existing key
let val1 = myobj["key1"]
if val1.isSome: echo "val1: ", val1.get
 
# test adding new key
myobj["key4"] = 4
let val4 = myobj["key4"]
if val4.isSome: echo val4.get
else: echo "val4 is empty"
 
# test reset and test whether its value is zero-ed
reset myobj
doAssert myobj["key3"].get == 0
 
main()
</syntaxhighlight>
{{output}}
<pre>
val1: 1
val4 is empty</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use strict;
<lang perl>package LockedHash;
 
use parent Tie::Hash;
package LockedHash;
use parent 'Tie::Hash';
use Carp;
use strict;
 
sub TIEHASH {
Line 270 ⟶ 713:
}
 
sub lock_hash :prototype(\%) {
my $ref = shift;
tie(%$ref, __PACKAGE__, %$ref);
Line 295 ⟶ 738:
# add a new key x: will die
eval { $h{x} = 1 };
if ($@) { print "Operation error: $@" }</langsyntaxhighlight>output:<syntaxhighlight lang="text">a => 3
b => 4
c => 5
Line 304 ⟶ 747:
operation error: Can't add key x at test.pl line 14
LockedHash::STORE('LockedHash=HASH(0x8cebe14)', 'x', 1) called at test.pl line 66
eval {...} called at test.pl line 66</langsyntaxhighlight>
=={{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 }
}
 
=={{header|Phix}}==
# Testing
There is no native "read-only" setting on phix dictionaries, so the following wraps a pair of them to
my $fh = FixedHash.new: "a" => 1, "b" => 2;
provide the requested functionality.
say $fh<a b>; # 1 2
<!--<syntaxhighlight lang="phix">(phixonline)-->
$fh<b>:delete;
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
say $fh<a b>; # 1 Nil
<span style="color: #008080;">enum</span> <span style="color: #000000;">STD</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">CUR</span>
$fh<b> = 42;
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fkds</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #000080;font-style:italic;">-- fixed key dictionaries ;-)</span>
say $fh<a b>; # 1 42
<span style="color: #004080;">integer</span> <span style="color: #000000;">freelist</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
say $fh<c>; # Nil
$fh<c> = 43; # error
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fkd_destroy</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
</lang>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span>
{{out}}
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">std</span><span style="color: #0000FF;">)</span>
<pre>(1 2)
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">)</span>
(1 (Any))
<span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">freelist</span>
(1 42)
<span style="color: #000000;">freelist</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">id</span>
can't store value for unknown key
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
in block <unit> at native-demonstration.p6:17
 
<span style="color: #008080;">function</span> <span style="color: #000000;">fkd_new</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">key_pairs</span><span style="color: #0000FF;">)</span>
Actually thrown at:
<span style="color: #004080;">integer</span> <span style="color: #000000;">std</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key_pairs</span><span style="color: #0000FF;">),</span>
in block <unit> at native-demonstration.p6:17</pre>
<span style="color: #000000;">cur</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">std</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">freelist</span>
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.
<span style="color: #008080;">if</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
 
<span style="color: #000000;">fkds</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fkds</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">})</span>
<lang perl6>class Magic {
<span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fkds</span><span style="color: #0000FF;">)</span>
has %.hash;
<span style="color: #008080;">else</span>
multi method FALLBACK($name, |c) is rw { # this will eat any extra parameters
<span style="color: #000000;">freelist</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span>
%.hash{$name}
<span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">}</span>
}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">id</span>
multi method FALLBACK($name) is rw {
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
%.hash{$name}
}
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fkd_clear</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
}
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span>
 
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">)</span>
my $magic = Magic.new;
<span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CUR</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">std</span><span style="color: #0000FF;">)</span>
$magic.foo = 10;
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
say $magic.foo;
$magic.defined = False; # error</lang>
<span style="color: #008080;">function</span> <span style="color: #000000;">fkd_get</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CUR</span><span style="color: #0000FF;">])</span>
{{output}}
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<pre>10
Cannot modify an immutable Bool
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
in block <unit> at native-demonstration.p6:15</pre>
<span style="color: #004080;">integer</span> <span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CUR</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">node</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span> <span style="color: #008080;">throw</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"invalid/new key"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CUR</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fkd_remove</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">std</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">node</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span> <span style="color: #008080;">throw</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"invalid key"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #000000;">std</span><span style="color: #0000FF;">),</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">fkd_sprint</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">cur</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CUR</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_all_keys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cur</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fkd_new</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"b"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">fkd_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {{"a",1},{"b",2}}</span>
<span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"b"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">66</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">fkd_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {{"a",55},{"b",66}}</span>
<span style="color: #000000;">fkd_clear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">fkd_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {{"a",1},{"b",2}}</span>
<span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"b"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">66</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">fkd_get</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 55</span>
<span style="color: #000000;">fkd_remove</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">try</span>
<span style="color: #000000;">fkd_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"NoNewKey"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">77</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span><span style="color: #0000FF;">[</span><span style="color: #004600;">E_USER</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- "invalid/new key"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">fkd_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {{"a",1},{"b",66}}</span>
<span style="color: #000000;">fkd_destroy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
from collections import UserDict
import copy
Line 448 ⟶ 924:
raise KeyError
else:
return super().setdefault(key, default)</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 455 ⟶ 931:
 
Implementation of functions that handle fenced-hash:
<syntaxhighlight lang="racket">
<lang Racket>
;(struct fenced-hash (actual original) ...)
 
Line 493 ⟶ 969:
;private custom-write ;mode is ignored
(write-string "#fenced-hash" port)
(write (hash->list (fenced-hash-actual dict)) port))</langsyntaxhighlight>
 
Definition of the actual structure and a “public” creator:
<langsyntaxhighlight Racketlang="racket">(struct fenced-hash (actual original)
#:extra-constructor-name *fenced-hash ;private constructor
#:omit-define-syntaxes ;not sure this is a good idea
Line 516 ⟶ 992:
(define (fenced-hash . args) ; public constructor
(define original (apply hash args))
(*fenced-hash (hash-copy original) original))</langsyntaxhighlight>
 
'''Example:''' Use the fenced-hash functions:
<langsyntaxhighlight Racketlang="racket">(define d (fenced-hash "a" 1 "b" 2))
 
(displayln d)
Line 531 ⟶ 1,007:
(displayln d)
(fenced-hash-remove! d "a")
(displayln d)</langsyntaxhighlight>
{{out}}
<pre>#fenced-hash(("b" . 2) ("a" . 1))
Line 540 ⟶ 1,016:
 
'''Example (continued):''' Use the same object as a dict. The dict-clear! method is not defined, so we must call fenced-hash-clear! instead.
<langsyntaxhighlight Racketlang="racket">(fenced-hash-clear! d)
(displayln d)
(dict-set! d "a" 55)
Line 551 ⟶ 1,027:
(displayln d)
(dict-remove! d "a")
(displayln d)</langsyntaxhighlight>
{{out}}
<pre>#fenced-hash(("b" . 2) ("a" . 1))
Line 558 ⟶ 1,034:
#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.
<syntaxhighlight lang="raku" line>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
</syntaxhighlight>
{{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.raku.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.
 
<syntaxhighlight lang="raku" line>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</syntaxhighlight>
 
{{output}}
<pre>10
Cannot modify an immutable Bool
in block <unit> at native-demonstration.p6:15</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Create an object/Native demonstration
 
map = []
map["A"] = 65
map["B"] = 66
map["C"] = 67
see map + nl
</syntaxhighlight>
Output:
<pre>
A
65
B
66
C
67
</pre>
 
=={{header|Ruby}}==
{{works with|Ruby|1.9}}
<langsyntaxhighlight lang="ruby"># A FencedHash acts like a Hash, but with a fence around its keys.
# One may change its values, but not its keys. Any attempt to insert
# a new key raises KeyError. One may delete a key, but this only
Line 804 ⟶ 1,356:
keys.map {|key| self[key]}
end
end</langsyntaxhighlight>
 
=={{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)].
<syntaxhighlight lang="scala">object CreateMapObject extends App {
val map = Map('A' -> 65, 'B' -> 66, 'C' -> 67)
 
println(map)
}</syntaxhighlight>
 
=={{header|Tcl}}==
This solution uses a dict(ionary), so requires Tcl 8.5 or better. Variable traces are used to detect write or unset access to such a protected variable, restore it to the backup value at protection time, and throw an exception
 
<langsyntaxhighlight Tcllang="tcl">proc protect _var {
upvar 1 $_var var
trace add variable var {write unset} [list protect0 $var]
Line 823 ⟶ 1,383:
puts "trying: $cmd"
if [catch {uplevel 1 $cmd} msg] {puts $msg}
}</langsyntaxhighlight>
Testing:
dict set dic 1 one
Line 844 ⟶ 1,404:
trying: unset dic
dic:1 one 2 two
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">class FixedSizeMap {
construct new(map) {
// copy the map so it cannot be mutated from the original reference
_map = {}
for (me in map.toList) _map[me.key] = me.value
}
 
containsKey(key) { _map[key] != null }
 
count { _map.count }
 
keys { _map.keys }
 
values { _map.values }
 
[key] { _map[key] }
[key] =(value) {
// do nothing if key doesn't already exist
if (_map[key] != null) _map[key] = value
}
 
reset(key) {
var t = _map[key].type
// leave unaltered if no suitable default value
_map[key] = (t == Num) ? 0 :
(t == String) ? "":
(t == Bool) ? false :
(t == List) ? [] :
(t == Map) ? {} : _map[key]
}
 
iterate(iterator) { _map.iterate(iterator) }
iteratorValue(iterator) { _map.iteratorValue(iterator) }
 
toString { _map.toString }
}
 
var map = { "a": 1, "b": 2 }
var fsm = FixedSizeMap.new(map)
System.print(fsm)
System.print(fsm.count)
fsm["a"] = 3
fsm["b"] = 4
System.print(fsm)
System.print(fsm.containsKey("c"))
fsm["c"] = 5 // attempt to add a new key/value pair
System.print(fsm) // ignored
fsm.reset("a")
System.print(fsm)
System.print(fsm.keys.toList)
System.print(fsm.values.toList)
for (me in fsm) System.print([me.key, me.value])
</syntaxhighlight>
 
{{out}}
<pre>
{b: 2, a: 1}
2
{b: 4, a: 3}
false
{b: 4, a: 3}
{b: 4, a: 0}
[b, a]
[4, 0]
[b, 4]
[a, 0]
</pre>
 
=={{header|zkl}}==
zkl has two dictionary objects: SD, a small dictionary that is created immutable and the "regular" dictionary has has a makeReadOnly method. They both behave the same when locked down.
<langsyntaxhighlight lang="zkl">d:=SD("one",1,"two",2);
d.keys; //-->L("one","two")
d["one"]; //-->1
d.add("three",3); // error thrown
d.pop("one") // error thrown</langsyntaxhighlight>
 
 
Line 860 ⟶ 1,489:
{{omit from|C|No associative arrays.}}
{{omit from|dc|No associative arrays.}}
{{omit from|Go|No operator overriding.}}
{{omit from|GUISS}}
{{omit from|PureBasic}}
9,476

edits