On this page

Wildcard and broadcast overrides๏ƒ

๐ŸŸ  Advanced ยท Lesson 19

THE OTHER TWO OVERRIDE FORMS. Lesson 01 taught the PATH form - "transport>credentials" - which names a socket exactly. There are three targeting forms in total, and melder states all of them:

PATH a>b>c name the socket exactly UNIQUE *param "there is exactly one of these, find it" BROADCAST **param "hit every one"

You still pass a plain dict to meld(override=...). The grammar lives in the KEY.

WHY THE OTHER TWO EXIST A path requires you to know the shape of a graph you did not build. *credentials says "somewhere below this root there is exactly one credentials socket - I do not care where". **credentials says "there may be several and I mean all of them".

AND HERE IS THE PART THAT MAKES THEM SAFE TO USE.

THE MATCH COUNTS ARE ENFORCED, NOT ADVISORY. *param requires EXACTLY ONE match **param requires AT LEAST ONE match Miss the count and resolution REFUSES. It does not apply your override to the first thing it found, and it does not quietly do nothing.

melder's own reasoning, which is worth reading twice:

"a *param that silently matched three sockets, or zero, would apply the caller's intent to the wrong object or to nothing at all, and BOTH FAIL INVISIBLY AT RUNTIME rather than at resolution. Failing loudly at map time is the whole point of resolving specs up front instead of during construction."

That is the never-substitute rule (lessons 06/11/12/15/16) applied to targeting. A wildcard that guessed would be the worst kind of bug: your fixture lands on the wrong object and everything appears to work.

PRECEDENCE A **param broadcast and an exact a>b>c path can both name the same socket. When they overlap, specificity decides - the exact path wins over the broadcast. Being more specific means being more authoritative, which is the only ordering that would not surprise someone.

THE LIFETIME RULE STILL APPLIES (lesson 01's sharp edge) An override is surgical in WHERE it reaches, not in HOW LONG it lasts. Override into unique and you have changed the world. Everything below is bound many so each meld builds its own graph and the fixtures cannot escape the call.

Before you run๏ƒ

Use the Advanced guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/03_advanced/19_wildcard_and_broadcast_overrides.py
py -3.14t UX_and_AIX_experiences/03_advanced/19_wildcard_and_broadcast_overrides.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

meld(override={"*param": obj}) and {"**param": obj}

Code๏ƒ

  1"""
  2TIER: advanced (19)
  3GOAL: THE OTHER TWO OVERRIDE FORMS. Lesson 01 taught the PATH form -
  4      "transport>credentials" - which names a socket exactly. There are
  5      three targeting forms in total, and melder states all of them:
  6
  7        PATH       a>b>c     name the socket exactly
  8        UNIQUE     *param    "there is exactly one of these, find it"
  9        BROADCAST  **param   "hit every one"
 10
 11      You still pass a plain dict to meld(override=...). The
 12      grammar lives in the KEY.
 13
 14      WHY THE OTHER TWO EXIST
 15      A path requires you to know the shape of a graph you did not build.
 16      `*credentials` says "somewhere below this root there is exactly one
 17      credentials socket - I do not care where". `**credentials` says
 18      "there may be several and I mean all of them".
 19
 20      AND HERE IS THE PART THAT MAKES THEM SAFE TO USE.
 21
 22      THE MATCH COUNTS ARE ENFORCED, NOT ADVISORY.
 23        *param   requires EXACTLY ONE match
 24        **param  requires AT LEAST ONE match
 25      Miss the count and resolution REFUSES. It does not apply your
 26      override to the first thing it found, and it does not quietly do
 27      nothing.
 28
 29      melder's own reasoning, which is worth reading twice:
 30
 31        "a `*param` that silently matched three sockets, or zero, would
 32         apply the caller's intent to the wrong object or to nothing at
 33         all, and BOTH FAIL INVISIBLY AT RUNTIME rather than at
 34         resolution. Failing loudly at map time is the whole point of
 35         resolving specs up front instead of during construction."
 36
 37      That is the never-substitute rule (lessons 06/11/12/15/16) applied
 38      to targeting. A wildcard that guessed would be the worst kind of
 39      bug: your fixture lands on the wrong object and everything appears
 40      to work.
 41
 42      PRECEDENCE
 43      A `**param` broadcast and an exact `a>b>c` path can both name the
 44      same socket. When they overlap, specificity decides - the exact
 45      path wins over the broadcast. Being more specific means being more
 46      authoritative, which is the only ordering that would not surprise
 47      someone.
 48
 49      THE LIFETIME RULE STILL APPLIES (lesson 01's sharp edge)
 50      An override is surgical in WHERE it reaches, not in HOW LONG it
 51      lasts. Override into `unique` and you have changed the world.
 52      Everything below is bound `many` so each meld builds its own graph
 53      and the fixtures cannot escape the call.
 54SURFACE EXERCISED: meld(override={"*param": obj}) and {"**param": obj}
 55VERIFY: rides the owner's 3.14t run; asserts are the contract.
 56
 57NOTE ON WHAT IS NOT TAUGHT HERE: `SpellOverrider` is the runtime helper
 58that maps this payload onto real sockets. It is marked AGENT_ACCESS:
 59internal - "users supply the override PAYLOAD, never this object" - so
 60this lesson teaches the DICT and never touches the class.
 61"""
 62import melder as md
 63
 64
 65class Credentials:
 66    def __init__(self) -> None:
 67        self.source = "vault"
 68
 69
 70class Transport:
 71    def __init__(self, credentials: Credentials) -> None:
 72        self.credentials = credentials
 73
 74
 75class Archive:
 76    def __init__(self, credentials: Credentials) -> None:
 77        self.credentials = credentials
 78
 79
 80class MailPipeline:
 81    """One credentials socket below the root, reached through transport."""
 82
 83    def __init__(self, transport: Transport) -> None:
 84        self.transport = transport
 85
 86
 87class BackupPipeline:
 88    """TWO credentials sockets below the root - transport and archive."""
 89
 90    def __init__(self, transport: Transport, archive: Archive) -> None:
 91        self.transport = transport
 92        self.archive = archive
 93
 94
 95def _book() -> md.Spellbook:
 96    # `many` throughout: every meld builds its own graph, so the fixtures
 97    # below cannot outlive the call that used them (lesson 01's edge).
 98    book = md.Spellbook(aetheric_frame="override-grammar")
 99    for spell in (Credentials, Transport, Archive, MailPipeline,
100                  BackupPipeline):
101        book.bind(spell=spell, existence="many")
102    return book
103
104
105def main() -> None:
106    conduit = _book().conjure(name="override-root")
107
108    fixture = Credentials()
109    fixture.source = "test-fixture"
110
111    # UNIQUE - *param. MailPipeline has exactly ONE credentials socket
112    # under it, so the wildcard resolves without naming the path.
113    mail = conduit.meld(
114        spell=MailPipeline,
115        override={"*credentials": fixture},
116    )
117    assert mail.transport.credentials is fixture
118    print("*credentials found the single socket:",
119          mail.transport.credentials.source)
120
121    # ...and the same spec against a root with TWO matching sockets is
122    # REFUSED. "exactly one" is a requirement, not a preference - this is
123    # the whole reason the form is safe to reach for.
124    try:
125        conduit.meld(
126            spell=BackupPipeline,
127            override={"*credentials": fixture},
128        )
129        raise AssertionError("expected a refusal - *param matched twice")
130    except Exception as error:
131        print("*credentials refused two matches:", type(error).__name__)
132
133    # BROADCAST - **param. Same graph, and now hitting every match is
134    # exactly what was asked for.
135    backup = conduit.meld(
136        spell=BackupPipeline,
137        override={"**credentials": fixture},
138    )
139    assert backup.transport.credentials is fixture
140    assert backup.archive.credentials is fixture
141    print("**credentials hit both sockets:",
142          backup.transport.credentials.source,
143          "/", backup.archive.credentials.source)
144
145    # A broadcast that matches NOTHING is refused too - "at least one" is
146    # also enforced. A no-op override is a caller mistake, not a default.
147    try:
148        conduit.meld(
149            spell=MailPipeline,
150            override={"**nosuchparam": fixture},
151        )
152        raise AssertionError("expected a refusal - **param matched nothing")
153    except Exception as error:
154        print("**nosuchparam refused zero matches:", type(error).__name__)
155
156    # PRECEDENCE. Broadcast everything, then name one socket exactly - the
157    # exact path is MORE SPECIFIC and wins where the two overlap.
158    specific = Credentials()
159    specific.source = "archive-only"
160    mixed = conduit.meld(
161        spell=BackupPipeline,
162        override={
163            "**credentials": fixture,
164            "archive>credentials": specific,
165        },
166    )
167    assert mixed.transport.credentials is fixture
168    assert mixed.archive.credentials is specific
169    print("overlap resolved by specificity - transport:",
170          mixed.transport.credentials.source,
171          "| archive:", mixed.archive.credentials.source)
172
173    # `many` kept the blast radius inside the call: a plain meld gets a
174    # clean graph with no fixture in it.
175    clean = conduit.meld(spell=BackupPipeline)
176    assert clean.transport.credentials is not fixture
177    assert clean.archive.credentials.source == "vault"
178    print("plain meld is untouched:", clean.archive.credentials.source)
179
180    print()
181    print("three forms: exact path, *one, **all - the grammar is in the key")
182    print("match counts are ENFORCED - a guessing wildcard would fail silently")
183
184
185if __name__ == "__main__":
186    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More advanced examples ยท Level guide