Get the item at key

var x = new HashMap[String, Int]
x["four"] = 4
assert x["four"] == 4

If the key is not in the map, provide_default_value is called (that aborts by default) See get_or_null and get_or_default for safe variations.

Property definitions

core $ MapRead :: []
	# Get the item at `key`
	#
	#     var x = new HashMap[String, Int]
	#     x["four"] = 4
	#     assert x["four"] == 4
	#     # assert x["five"] #=> abort
	#
	# If the key is not in the map, `provide_default_value` is called (that aborts by default)
	# See `get_or_null` and `get_or_default` for safe variations.
	fun [](key: nullable Object): V is abstract
lib/core/collection/abstract_collection.nit:534,2--543,44

core $ CoupleMap :: []
	redef fun [](key)
	do
		var c = couple_at(key)
		if c == null then
			return provide_default_value(key)
		else
			return c.second
		end
	end
lib/core/collection/abstract_collection.nit:1286,2--1294,4

counter $ Counter :: []
	# The number of counted occurrences of `e`
	redef fun [](e)
	do
		var map = self.map
		if map.has_key(e) then return map[e]
		return 0
	end
lib/counter/counter.nit:55,2--61,4

trees $ Trie :: []
	redef fun [](key) do
		if cache_key == key then return cache.as(not null).value.as(not null)
		var node = search_node(key)
		assert node != null
		return node.value
	end
lib/trees/trie.nit:99,2--104,4

core $ HashMap :: []
	redef fun [](key)
	do
		var c = node_at(key)
		if c == null then
			return provide_default_value(key)
		else
			return c._value
		end
	end
lib/core/collection/hash_collection.nit:226,2--234,4

trees $ BinTreeMap :: []
	# Get the node value associated to `key`
	# O(n) in worst case, average is O(h) with h: tree height
	#
	#     var tree = new BinTreeMap[Int, String]
	#     for i in [4, 2, 1, 5, 3] do tree[i] = "n{i}"
	#     assert tree.has_key(1)
	#     assert tree[1] == "n1"
	#     assert tree.has_key(1)
	#     assert tree[2] == "n2"
	redef fun [](key) do
		assert not_empty: not is_empty
		if cache_node != null and cache_node.key == key then return cache_node.value
		var res = search_down(root.as(not null), key)
		assert has_key: res != null
		return res.value
	end
lib/trees/bintree.nit:80,2--95,4

core $ ArrayMap :: []
	# O(n)
	redef fun [](key)
	do
		var i = index(key)
		if i >= 0 then
			return _items[i].second
		else
			return provide_default_value(key)
		end
	end
lib/core/collection/array.nit:694,2--703,4

gamnit $ ShaderVariableMap :: []
	redef fun [](key)
	do
		# Alter the user specified name to fit the truncated name
		var max_len = max_name_length - 1
		if key isa Text and key.length > max_len then key = key.substring(0, max_len)
		return super(key)
	end
lib/gamnit/programs.nit:515,2--521,4

ini $ IniSection :: []
	# Get the value associated with `key`
	#
	# Returns `null` if the `key` is not found.
	#
	# ~~~
	# var section = new IniSection("section")
	# section["key"] = "value1"
	# section["sub.key"] = "value2"
	#
	# assert section["key"] == "value1"
	# assert section["sub.key"] == "value2"
	# assert section["not.found"] == null
	# ~~~
	redef fun [](key) do
		if not has_key(key) then return null
		return super
	end
lib/ini/ini.nit:536,2--552,4

vsm $ Vector :: []
	redef fun [](k) do
		if not has_key(k) then return 0.0
		return super
	end
lib/vsm/vsm.nit:75,2--78,4

gamnit $ AttributeMap :: []
	redef fun [](key)
	do
		# Update the location of this attribute from the user specified name
		var item = super
		if key isa Text then item.location = program.gl_program.attrib_location(key.to_s)
		return item
	end
lib/gamnit/programs.nit:536,2--542,4

gamnit $ UniformMap :: []
	redef fun [](key)
	do
		var item = super
		if key isa Text then item.location = program.gl_program.uniform_location(key.to_s)
		return item
	end
lib/gamnit/programs.nit:555,2--560,4

ini $ IniFile :: []
	# Get the value associated with a property (`key`)
	#
	# Returns `null` if the key is not found.
	# Section properties can be accessed with the `.` notation.
	#
	# ~~~
	# var ini = new IniFile.from_string("""
	# key=value1
	# [section1]
	# key=value2
	# [section2]
	# key=value3
	# """)
	# assert ini["key"] == "value1"
	# assert ini["section1.key"] == "value2"
	# assert ini["section2.key"] == "value3"
	# assert ini["section1"] == null
	# assert ini["not.found"] == null
	# ~~~
	redef fun [](key) do
		if key == null then return null
		key = key.to_s.trim

		# Look in root
		var node = node_at(key)
		if node != null then return node.value

		# Look in sections
		for section in sections do
			# Matched if the section name is a prefix of the key
			if not key.has_prefix(section.name) then continue
			var skey = key.substring(section.name.length + 1, key.length)
			if section.has_key(skey) then return section[skey]
		end
		return null
	end
lib/ini/ini.nit:198,2--233,4