Last updated: 2026-11-15

U
Undergraduate level
FDN
Foundational โ€” Knowledge that endures for decades โ€” core principles

Values, Objects and Operator Events in PatLang 2.0

For new readers

In PatLang 2.0 every value, from the integer 3 to a user's own class instance, is an object of a class and answers methods through it. Operators are methods too, so 3 + 5 sends + to 3, and a method can announce what it did as an ordinary event that any part of the program can handle. This page sets out those rules, the split between values that copy and objects that share, how text is counted, and what the language promises about memory.the runtime enforces this split via ownership rules

The rules here are Part 1 of PatLang's values-and-memory specification, sections 1.1 to 1.7 and 1.10. Part 1 is what a program can observe, and it binds the compiler and the interpreter alike: every example on this page prints the same on both.

Every value is an object FoundationalKnowledge that endures for decades โ€” core principles

Every value carries its class, and type_of(v) names it. The built-in classes are int, bigint, float, rational, complex, interval, bool, unit, string, list, closure, string_builder, vec, bool_array, rule_store and modint. Any other class is one a class declaration or new gave. No value is ever classless, and no operation changes a value's class without producing a new value.immutable values: no mutation in place

Methods work on every value through its class, on a literal as on a variable. A built-in class's methods are declared in PatLang, in the runtime's source, by the same mechanism as a user class's.

let w = new("Widget", "w1")
print(type_of(w))
print(3.to_s + "!")
print([1, 2, 3].length)
Widget
3!
3

new returns the object. In v1 it returned the name, here "w1", and get, send and object_delete found the object by that name. The name is still a way to find a named object: where s is a string holding the name, s.field reads the field and s.field = x sets it. new takes a class and an optional name, and further arguments stop the program, since inheritance and traits belong to a class declaration.

Operators are methods FoundationalKnowledge that endures for decades โ€” core principles

An operator is a method of its left operand's class: 3 + 5 sends + to 3 with the argument 5. A class gives itself an operator by declaring a method of that name.

class Money {
  field cents = 0
  make a function called + takes self, other returns r raises money_added
    let m = new("Money")
    m.cents = self.cents + other.cents
    return m
  end
}
when money_added do
  print("added " + event_data[1].cents.to_s + " to " + event_data[0].cents.to_s)
end
let a = new("Money")
a.cents = 150
let b = new("Money")
b.cents = 275
let total = a + b
print(type_of(total))
print(total.cents)
added 275 to 150
Money
425

The raises money_added clause, after returns, names an event the method raises once it has its result. The events are ordinary PatLang events, raised and handled as any event is with emit and when, with nothing specific to operators. A raised event's event_data is a list of the receiver, the arguments and the result, in that order, so the handler above reads event_data[0] for a and event_data[1] for b.the receiver is the object the method lives on

The events integers raise Applied / MethodologicalKnowledge with a 5โ€“10 year half-life โ€” stable practice

int's + raises added with event_data [3, 5, 8]. So that a handler can see "3 was added to 5" and "5 was added to 3" as well as "3 and 5 were added", it also raises added_to once for each operand, with event_data [operand, other operand]. The order is fixed: the receiver's added_to, the argument's added_to, then added, all after the result is computed. A handler cannot change the result.

That leaves one trap, which writing the specification's scenarios found: a handler for added that itself adds two integers would raise added again and never return. The rule is that while a handler for an event runs, nothing it does on the same thread raises that event again. Other events are raised as usual:

when added_to do
  print(["added_to", event_data])
end
when added do
  let check = 10 + 20
  print(["added", event_data])
end
let r = 3 + 5
print(r)
[added_to, [3, 5]]
[added_to, [5, 3]]
[added_to, [10, 20]]
[added_to, [20, 10]]
[added, [3, 5, 8]]
8

The handler's own 10 + 20 raised the two added_to events, which another handler printed, and did not raise added. A handler running on a fiber holds back its event on that fiber only. If it yields, whoever resumed the fiber raises the event as usual, since each fiber keeps its own list of the events whose handlers are running.

Handlers register on an event, never on an individual value, so an integer needs no state of its own to support any of this. A program with no handler pays almost nothing: the compiled code for a + b on two ints is a test of one flag and a machine add, and the compiler leaves out even the flag test when no when in the program handles added or added_to. The runtime's own source files declare quiet_operators(), so the runtime's internal arithmetic never raises a program's events.zero-cost abstractions: the flag vanishes if unused

Values and objects with identity FoundationalKnowledge that endures for decades โ€” core principles

The language divides its values into two kinds, and the difference decides whether a change made in one place is seen in another.

KindClassesA "change"Equality
Valuesnumbers, booleans, unit, strings, lists, closuresProduces a new value. Every other holder keeps seeing the old one.Equal when the contents are (closure equality is unspecified)
Objects with identityclass instances, objects made by new, string builders, vecs, bool arrays, rule storesSeen through every holder.Equal only if they are the same object
let xs = [1, 2]
let ys = list_push(xs, 3)
print(xs)
print(ys)
let v = vec_new()
let u = v
vec_push(u, "seen")
print(vec_len(v))
print([1, 2] == [1, 2])
let o1 = new("Widget")
let o2 = new("Widget")
print(o1 == o2)
print(o1 == o1)
[1, 2]
[1, 2, 3]
1
true
false
true

Value semantics would be expensive if every push copied the list, and v2 does not. When nothing else holds the old list, let xs = list_push(xs, e) takes the same time however long xs is, also when e itself reads xs. The same holds for let s = s + x on a string nothing else holds. The runtime can tell because it counts references: a list whose count is 1 is changed in place, and any other is copied first. Inside a compiled program describes the counting.

Text Applied / MethodologicalKnowledge with a 5โ€“10 year half-life โ€” stable practice

A string holds characters, whatever bytes they take. .length, indexing, substr, char_code, chr and the string cursor (str_intern, sc_len, sc_code, sc_char, sc_substr) all count characters; byte_length and hash_string see the UTF-8 bytes.

let s = "hรฉllo"
print(s.length)
print(byte_length(s))
print(chr(233))
5
6
รฉ

An ASCII string indexes in constant time. Any other walks from its start, so a loop that scans a non-ASCII string character by character should use the cursor, which keeps its place. chr(n) takes an int and nothing else; v1 truncated a float silently.

sed(command, input) is new: it takes s/pattern/replacement/flags and replaces the first literal match of the pattern, or every match with g, matching letters in either ASCII case with i. The pattern is literal text, not a regular expression.literal match, not regex so no wildcards

What the language promises about memory FoundationalKnowledge that endures for decades โ€” core principles

  • No program frees memory, and none can observe when memory is freed.
  • A value lives as long as something holds it. A program whose live data is bounded runs in bounded memory however long it runs, so a loop that replaces a value on each iteration does not grow with the number of iterations.
  • Named objects, made by new with a name, are the one exception: they live until object_delete. Finding one by its name takes about the same time however many there are, and a repeated name finds the earliest of its objects still alive.
  • A program has no primitive for reading its own memory use. The bound is checked from outside, by the process's peak working set, which is how the bounded_memory scenarios test it: each loop runs for N and for 10N iterations and the peak must not grow.

Named objects took quadratic time in an early v2 build, 49.7 seconds for 8,000 of them, against 84 milliseconds in v1. They now take 0.63 seconds at 8,000, growing about linearly. Prefer unnamed objects, new("Camera"), unless something must find the object by name; an unnamed object goes when its last holder does.

The two engines agree FoundationalKnowledge that endures for decades โ€” core principles

The interpreter and native code are two routes to one language, and the specification requires every Part 1 scenario to give the same observable result on both, whichever directory the program is started in. A program file or an included file that cannot be read is an error on both, naming the file, and nothing of the program runs. In v1 a missing program exited 0 with no output, and a missing include was ignored.

The v2 interpreter runs the runtime's compiled functions directly and lowers programs with the compiler's own front end, so much of this agreement holds by construction. The rest is tested: engines_agree.feature makes 62 checks, each running a program on both engines and comparing the output.one front end, many back ends

See also

Functions, calls and parameters covers the call rules that methods share with functions. Numbers in PatLang 2.0 covers the numeric classes in detail. Blocks, ownership and memory explains why the design review chose objects and events in this form. What changed since v1 lists every change in one place.

Graduate Attributes

Modelled on this page

  • Subject Specialist: Technical distinctions between value semantics, object identity and memory management are defined with precise rules.
  • Researcher: Performance benchmarks and test counts are cited to validate claims about execution speed and engine consistency.
  • Critical Thinker: Potential infinite loops caused by event handlers are identified and resolved with specific threading constraints.

University of Reading Graduate Attributes framework.