Home / Docs / Standard library / Built-in methods

has

Release v0.4.0Edition 0.2 and later

A built-in method, resolved by the type of its receiver (spec/14 §2). Edition 0.2 and later.

Edition 0.2 and later

una
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

una
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: BitSet

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

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

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

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

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

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

Returns

Bool: true when item is a member of b; not usable in contracts in edition 0.5.

Use instead

Cost

Time O(log n) per lookup on a Map[K, V] (spec/21 §5), where:

Example

src/shop.unauna
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:

Output
lamp: true
chair: false

Specification

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.