1.7. Records

A record is an unordered finite map from keys to values. The empty record is written as {. .}.

1.7.1. Syntax

[5]Record::= {. Key = Expression , , Key = Expression .}  

1.7.2. Constructors

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.

1.7.3. Operations

Notable record operations include:

  • Publish the value bound to key k in record r (a dot access): r.k

  • Extend a record r with new entries : r + s

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 .}.

1.7.4. Special Keys

There are two record keys that have special meanings:

1.7.5. Type

The type of a record value {. K0 = v0 ,, Kn = vn .} where vi has type Ti, is a record type, written {. K0 :: T0 ,, Kn :: Tn .}.

1.7.6. Java calls

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.

1.7.7. Examples

Normalizing Vectors
{- 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 .}
-}

Record Extension
{- 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
-}

1.7.8. Related Links

Related Reference Topics

Related Tutorial Sections