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.
# 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
# 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
# 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
# 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