View Problem

Define an unmodifiable empty map

DiskEdit
clojure
; Clojure maps are immutable
(def m {})
DiskEdit
cpp
const std::map<T1,T2> immutable_map_instance_of_type_t1_to_t2;
ExpandDiskEdit
erlang

% Erlang data structures are immutable - updating a 'map' sees a modified copy created
Map = dict:new(),
ExpandDiskEdit
fantom
map := [:].ro
ExpandDiskEdit
fsharp
// Most native fsharp data structures are immutable - updating a 'map' sees a modified copy created
let map = Map.empty
DiskEdit
groovy
empty = Collections.EMPTY_MAP
DiskEdit
groovy
map = [:].asImmutable()
ExpandDiskEdit
groovy with commons collections
def empty = MapUtils.EMPTY_SORTED_MAP
ExpandDiskEdit
groovy with google collections
def empty = ImmutableMap.of()
DiskEdit
haskell
import qualified Data.Map as Map

output :: Map.Map k v
output = Map.empty
ExpandDiskEdit
java
Map empty = Collections.EMPTY_MAP;
ExpandDiskEdit
java org.apache.commons
SortedMap empty = MapUtils.EMPTY_SORTED_MAP;
DiskEdit
ocaml
(* OCaml maps are functional data structures (so are immutable) *)
module StringMap = Map.Make (String)
let m = StringMap.empty
DiskEdit
perl
# perl does not provide unmodifiable maps/hashes, but you could use "constant
# functions", if you really need them
# 2011-07-06 Not actually true, see Hash::Util::lock_hash;

sub MAP () { {} }
DiskEdit
perl
use Hash::Util qw/lock_hash/;
# two lines
my %hash;
lock_hash(%hash);
# or in one line
lock_hash(my %locked_hash);
ExpandDiskEdit
php
/* It's not possible to define an array as a constant
* Instead we can use the serialize-function */

$fruits = array("apple", "banana", "orange");
define("FRUITS", serialize($fruits));
echo FRUITS; // a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}
$my_fruits = unserialize(FRUITS); // and normal array again
DiskEdit
python 2.6
import collections
EmptyDict = collections.namedtuple("EmptyDict", "")
e = EmptyDict()
DiskEdit
ruby
map = {}.freeze
ExpandDiskEdit
scala
val map = immutable.Map.empty
ExpandDiskEdit
scala
val map = immutable.TreeMap.empty[String, Int]
DiskEdit
scala
val map = Map()

Submit a new solution for clojure, cpp, erlang, fantom ...