Built with Bend
9 views in the last 24 hours
← Blog
3 min read Built with Bend

Bend 2 Map Lookup: Missing Key or Stored Zero?

Test Map.get and Map.has on the same Bend 2 keys. Change the fallback to see which zero was stored, then compare Base with MyLSM’s durable lookup.

A retry limit of zero can mean “don’t retry.” A missing retry limit might mean “use the application default.” In Bend 2, Map.get can return zero in both cases: it takes a fallback value, not a request to tell you whether the key exists. Use Map.has when the distinction affects what your program does.

Here’s a way to see the difference without installing a package or building a database. We ran the same two-key probe twice on Bend 2.0.40, changing just the fallback between runs. Only one answer moved.

Read the two return values

Run bend version first. The example uses the Base library from the Bend 2.0.40 release, not the original HVM-based Bend. With that compiler installed, run bend base Map to see the definitions shipped with your executable. The versioned language guide describes the lookup convention: both operations return the map alongside their answer.

For the natural-number map below, read the calls this way:

  • Map.has(&2, Nat, m, key) returns a pair: the map and a Boolean. It answers “was this string key present?”
  • Map.get(Nat, 0n, m, key) returns the map and a natural number. Its second argument, 0n, is the number to use if the key is absent. It is not a value to insert.

Follow the first component if you need the map again. In the example, read_key receives the pair from Map.has, names its components m and present, and passes that returned map to Map.get. The little value function discards the map only after the final lookup.

Give the missing key the same answer as the stored key

Save this as map.bend. The configuration contains exactly one entry: the string key "retries" with the natural number 0n. The second probe asks about "timeout", which we deliberately never set.

import Base

def config() -> Map<&2, Nat>:
  empty = Map.new(&2, Nat)
  Map.set(&2, Nat, empty, "retries", 0n)

def value(pair: Map<&2, Nat> & Nat) -> Nat:
  (m, n) = pair
  n

def read_key(key: String, pair: Map<&2, Nat> & Bool) -> Bool & Nat:
  (m, present) = pair
  (present, value(Map.get(Nat, 0n, m, key)))

def probe(+key: String) -> Bool & Nat:
  read_key(key, Map.has(&2, Nat, config(), key))

def main() -> (Bool & Nat) & (Bool & Nat):
  (probe("retries"), probe("timeout"))

Run bend map.bend. Our 2.0.40 executable printed:

((True{}, 0n), False{}, 0n)

Read that against the type of main: the left probe reports present, zero; the right probe reports absent, zero. Each probe starts with the same one-entry configuration. Testing only the two numbers would give you two passing assertions even if you accidentally changed the first lookup from "retries" to "retrise". Checking the Boolean catches that typo.

Change the fallback, not the data

Change just Map.get(Nat, 0n, m, key) to Map.get(Nat, 9n, m, key). Leave the stored retry limit at zero. Running the edited file gave us:

((True{}, 0n), False{}, 9n)

The stored zero survived; only the missing key picked up the nine. That makes a useful test for configuration readers: pick a stored value equal to your normal fallback, include an absent key, then vary the fallback. If both results change, check the key you inserted and the map you passed into the read. If you mean to treat missing and explicitly zero as equivalent, a single get is enough; don’t add a presence check that has no effect on the decision.

When the value has to survive a restart

This probe starts over on every run. If you need a saved key rather than a value inside one calculation, compare the MyLSM directory entry and its durable API contract. MyLSM documents a different lookup: open a database, pass its handle and a key, and inspect a result that distinguishes a value from absence. A handle-preserving operation hands the handle back for the next operation. There is no caller-supplied numeric fallback in that documented lookup. We checked the contract, not MyLSM’s persistence at runtime.

For another Bend library, inspect the actual definition before reusing this test. JendHub is a directory starting point for definition search; the package-import walkthrough shows how to follow a definition to versioned source. Look for three things in the signature: who supplies the fallback, how absence is represented, and what you must carry into the next call.

Curated by LVTDBuilt for the people building.