ddonche/goblin-lang
0.46.24
1
0
example documentation
[[broadcast-tether]]

Broadcast Tether


In Goblin, a broadcast tether lets you tether multiple names to the same value in a single statement.

It uses the same tether/retether rules as normal | and |=—you’re simply doing it for a comma-separated list of names.


Broadcast Tether

What you write

a, b, c, d | "something"
a, b, c, d | "something"

What it does

All names on the left are tethered to the same value:

a  /// -> "something"
b  /// -> "something"
c  /// -> "something"
d  /// -> "something"
a  /// -> "something"
b  /// -> "something"
c  /// -> "something"
d  /// -> "something"

This works with any right-hand value that is not an array destructure.

Examples:

age, price_in_dollars, answer_to_ultimate_question | 42
x, y, z | nil
age, price_in_dollars, answer_to_ultimate_question | 42
x, y, z | nil


Broadcast Retether

Broadcast retether does the same thing, but with |=.

What you write

a, b |= 9
a, b |= 9

Rules

  • Every name on the left must already exist (somewhere in scope).
  • If any name is unknown, you get an error (same as normal |=).
  • If any name is immutable, you get an error (same as normal |=).

How it relates to Tuple Bind

Tuple bind is the other thing you can do with commas on the left.

Tuple bind happens when the RHS1 is an array with values:

a, b | [1, 2]
a, b | [1, 2]

That is destructuring, not broadcasting.

Arity

If the array length doesn’t match the number of names, you get a wrong-arity error:

a, b | [1, 2, 3]  /// wrong-arity (expected 2, got 3)

error: R0301: wrong-arity: tuple binding expected 2 value(s) but got 3
  ┌─ <repl>:1:6
  = help: Make the array length match the number of names on the left (or use [] to broadcast an empty array).
  = link: https://goblinlang.org/docs/errors#R0301
a, b | [1, 2, 3]  /// wrong-arity (expected 2, got 3)

error: R0301: wrong-arity: tuple binding expected 2 value(s) but got 3
  ┌─ <repl>:1:6
  = help: Make the array length match the number of names on the left (or use [] to broadcast an empty array).
  = link: https://goblinlang.org/docs/errors#R0301

Special case: empty array broadcast

An empty array is treated as a broadcast empty array:

lines, rel_lines, parts_out | []
lines, rel_lines, parts_out | []

That tethers each name to an empty array. This is equivalent to broadcasting to an empty string "" or nil but for arrays.


Why this exists

This isn’t "assignment" in the usual sense.

Goblin treats names as tethers:

  • | tethers a new name to a value
  • |= retethers an existing name to a new value

Broadcast tether is simply doing that tethering in bulk, which is useful for initializing several variables cleanly at the top of an action.


  1. right-hand side