Content deleted Content added
Line 94:
=== Extensible Records ===
The following program fragment shows how to construct an immutable record with two fields <code>x</code> and <code>y</code>:
<syntaxhighlight lang="Scala">
def point2d(): {x: Int, y: Int} = {x = 1, y = 3}
</syntaxhighlight>
Flix uses [[row polymorphism]] to type records. The following program fragment shows how to write a function that works with any record that has <code>x</code> and <code>y</code> fields:
<syntaxhighlight lang="Scala">
def sum(r: {x: Int, y: Int | _}): Int = r.x + r.y
</syntaxhighlight>
The following are all valid calls to the <code>sum</code> function:
<syntaxhighlight lang="Scala">
sum({x = 1, y = 2}) // returns 3
sum({y = 2, x = 1}) // returns 3
sum({a = 1, x = 2, z = 3, y = 4}) // returns 6
</syntaxhighlight>
|