A record is an unordered finite map from keys to values.
The empty record is written as {. .}
.
A record is constructed from a comma-separated sequence of bindings enclosed by dotted braces. Each binding associates a key with an expression.
The record expression {.
K0
=
E0
,
… ,
Kn
=
En
.}
publishes the record {.
K0
=
v0
,
… ,
Kn
=
vn
.}
only if each expression Ei
deflates to value vi
.
Otherwise, it halts silently.
Duplicate bindings for the same key are allowed, but only the rightmost binding will be used in the resulting record value.
Notable record operations include:
Publish the value bound to key k
in record r
(a dot access): r
.
k
A record extension r
+
s
publishes a new record with all of the bindings of s
,
plus all of the bindings in r
which do not bind keys mentioned in s
.
In other words, s
overrides r
.
Record extension is associative, but not commutative.
The expression
{.
K0
=
E0
,
… ,
Kn
=
En
.}
is equivalent to the expression
{.
K0
=
E0
.}
+
… +
{.
Kn
=
En
.}
.
There are two record keys that have special meanings:
If a record has a binding for the apply
key, then the record may be called like a site or function.
If a record has a binding for the unapply
key, then the record may be used in a call pattern.
The type of a record value {.
K0
=
v0
,
… ,
Kn
=
vn
.}
where vi
has type Ti
,
is a record type, written {.
K0
::
T0
,
… ,
Kn
::
Tn
.}
.
Orc records do not correspond to any Java value, so if a record is passed to Java code, it will be as a java.lang.Object
of a type not specified here.
{- Normalize a given integer vector using records -} def magnitude(v) = (v.x * v.x + v.y * v.y + v.z * v.z) ** 0.5 def norm(v) = val m = magnitude(v) {. x = v.x / m, y = v.y / m, z = v.z / m .} val velocity = {. x = 3.0, y = 0.0, z = 4.0 .} norm(velocity) {- OUTPUT: {. x = 0.6, y = 0, z = 0.8 .} -}
{- Add an alpha field to an rgb color record -} val rgb = {. red = 60, green = 230, blue = 5 .} val rgba = rgb + {. alpha = 128 .} rgba.alpha {- OUTPUT: 128 -}
Related Reference Topics
Related Tutorial Sections