Ref
A Ref is a token that no other Ref of the run equals, like make_ref() in Erlang and Elixir. Values are compared by structure, so two equal values are one value; put a Ref in a field to tell apart values that are otherwise equal, such as two nodes of a graph with the same operation and arguments.
==and!=compare Refs: a Ref equals only itself.- A Ref is a
Mapkey, aSetelement, anATSkey, and an item ofuniq,tallyandgroup_by. sortandsort_byorder Refs by when they were made: a Ref made after another sorts after it, in the same task or in a task that got the first one through a channel orTask.await. So when each value is made after the values it uses, sorting their Refs gives a topological order.- A Ref is data: a struct field, an enum payload, a list item, a channel message or an
ATSvalue. - A Ref prints as
<Ref 1>, the number of its creation in the run, for debugging. No function turns a number or a text into a Ref, and a Ref has no arithmetic, no<, no conversion toIntand no JSON form (CHECK-TYPE-MISMATCH,CHECK-NOT-ENCODABLE). Refis not a reserved name: a file that declares or imports its own typeRef(such asstd/actor) means that type when it writesRef, andRef.new()still makes a token.
Ref.new #
Ref.new() -> RefReturns a new Ref. Tasks on any scheduler thread make Refs at once without a lock, and each one differs from every other Ref of the run.
struct Node
id: Ref
name: String
end
def main() -> Void
a = Node(id: Ref.new(), name: "a")
b = Node(id: Ref.new(), name: "a")
puts(a == b)
puts(a == a)
seen = Set.new([a.id])
puts(seen.include?(b.id))
puts([b, a].sort_by { |n| n.id } == [a, b])
puts(a.id)
endOutput
false
true
false
true
<Ref 1>