Home / Docs / Standard library / Built-in methods
has
A built-in method, resolved by the type of its receiver (spec/14 §2). Edition 0.2 and later.
Edition 0.2 and later
m.has(key: K) -> Bool
where m: Map[K, V]Whether m has key. Like m.len, it may appear in requires, ensures, where and predicates (spec/21 §2, spec/21 §4). On a Set, from edition 0.5 on, whether s holds item, in O(log n) (spec/28 §6). On a HashMap and a HashSet, the same in O(|k|) expected, outside contracts in edition 0.5 (spec/28 §7). On a BitSet, whether item is a member, false outside its range, in O(1) (spec/28 §9).
Edition 0.5 preview
m.has(key: K) -> Bool
where m: Map[K, V]
s.has(item: T) -> Bool
where s: Set[T]
m.has(key: K) -> Bool
where m: HashMap[K, V]
s.has(item: T) -> Bool
where s: HashSet[T]
b.has(item: Int) -> Bool
where b: BitSetWhether m has key. Like m.len, it may appear in requires, ensures, where and predicates (spec/21 §2, spec/21 §4). On a Set, from edition 0.5 on, whether s holds item, in O(log n) (spec/28 §6). On a HashMap and a HashSet, the same in O(|k|) expected, outside contracts in edition 0.5 (spec/28 §7). On a BitSet, whether item is a member, false outside its range, in O(1) (spec/28 §9).
In editions 0.5. In edition 0.2, 0.3, 0.4: no Set[T], HashMap[K, V], HashSet[T] or BitSet receiver, since std.collections begins in edition 0.5: it has none of s.has(item: T) -> Bool, where s: Set[T], where m: HashMap[K, V], where s: HashSet[T], b.has(item: Int) -> Bool and where b: BitSet.
On a Map[K, V]
The receiver is the map m.
Parameters
key: the key to look for; from edition 0.3, it is taken at the base type of a refined key typeK, so a key outside the refinement finds no entry
Returns
Bool: true when m has an entry at key; usable in requires, ensures, where and predicates.
On a Set[T]
The receiver is the set s.
Parameters
item: the item to look for; it is taken at the base type of a refined item typeT, so an item outside the refinement is not found
Returns
Bool: true when s holds item; not usable in contracts in edition 0.5.
On a HashMap[K, V]
The receiver is the hash map m.
Parameters
key: the key to look for; it is taken at the base type of a refined key typeK, so a key outside the refinement finds no entry
Returns
Bool: true when m has an entry at key; not usable in contracts in edition 0.5.
On a HashSet[T]
The receiver is the hash set s.
Parameters
item: the item to look for; it is taken at the base type of a refined item typeT, so an item outside the refinement is not found
Returns
Bool: true when s holds item; not usable in contracts in edition 0.5.
On a BitSet
The receiver is the bit set b.
Parameters
item: the integer to look for; outside the range from 0 tob.size - 1it is never a member
Returns
Bool: true when item is a member of b; not usable in contracts in edition 0.5.
Use instead
getto read the value too
Cost
Time O(log n) per lookup on a Map[K, V] (spec/21 §5), where:
n: the number of entries inm
Example
module shop
--- Whether an item is stocked at all.
pub main() -> Unit
uses @console: Console[print]
=
let stock = empty_map().with(key: "lamp", value: 3).with(key: "desk", value: 1)
let lamp = stock.has(key: "lamp")
@console.print("lamp: {lamp}")
let chair = stock.has(key: "chair")
@console.print("chair: {chair}")una run . prints:
lamp: true
chair: falseSpecification
- spec/21 §2
- spec/21 §4
spec/28 §6spec/28 §7spec/28 §9
Generated from una v0.4.0 (docs/std/builtins.json, which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: spec/14 §2.