---
title: "has"
url: https://una-lang.org/en/docs/std/builtin/has/
release: v0.4.0
edition: "0.2 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/builtins.json
---

# has

A built-in method, resolved by the type of its receiver ([`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2)). 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

- `key`: the key to look for\; from edition 0\.3\, it is taken at the base type of a refined key type `K`\, 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 type `T`\, 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 type `K`\, 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 type `T`\, 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 to `b.size - 1` it is never a member

### Returns

`Bool`: `true` when `item` is a member of `b`\; not usable in contracts in edition 0\.5.

## Use instead

- [`get`](https://una-lang.org/en/docs/std/builtin/get/) to 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 in `m`

## Example

`src/shop.una`

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

```text
lamp: true
chair: false
```

## Specification

- [`spec/21 §2`](https://una-lang.org/en/docs/spec/21-maps/#s2)
- [`spec/21 §4`](https://una-lang.org/en/docs/spec/21-maps/#s4)
- `spec/28 §6`
- `spec/28 §7`
- `spec/28 §9`

Generated from una v0.4.0 ([`docs/std/builtins.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/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`](https://una-lang.org/en/docs/spec/14-standard-library/#s2).
