Planet JFX

Sequences are Tuples

118pages on
this wiki
Add New Page
Add New Page Talk0

This is an alternative to the Tuples_and_Arrays proposal, though they share some ideas and features.

A sequence is an ordered list of items. Sequences are flat: they do not nest. A sequences with a single item is the same as that item.

Comma is a binary operator for constructing sequences:

1,2 ==> a sequence with two values
1,2,3 ==> a sequence with 3 values

Comma is associative:

1,2,3 is the same as (1,2),3 or 1,(2,3)

The zero-item sequence is written: (). The zero-item sequence is the same as the "void value". Parens are just grouping: ((x)) is the same as (x) which is the same as x if x is a "primary",

Sequences are first-class: The value of a variable, attribute, expression, etc is sequence, with 0, 1, or many items.

A function parameter list is a sequence:

 function f(x, y) { "x is {x} and y is {y}" }
 let a = (2,3);
 f(a) ==>  "x is 2 and y is 3"

So how can we assign a non-singleton sequence to a function parameter? The answer: Pattern matching:

 function f(x : int, y : int, rest: int*)
 { "x is {x} and y is {y} and rest is {rest}" }
 f(2,3,4,5) ==> "x is 2 and y is 3 and rest is (4,5)"

Basically the formal parameter list is a kind of regular expression that is matched against the actual parameter tuple.

(See Patterns for more on pattern matching, but note that page assumes the Tuples_and_Arrays, and so it doesn't need general "regular-expression"-matching.)

If a formal parameter is a simple name, the default type is "any single item", so it only matches one item.

But using pattern matching to extract sub-sequences from a parameter list is awkward. We need some way of more directly grouping some values with a specific parameter, which is difficult without nesting. But we can replace direct nesting by indirect nesting, by wrapping an inner sequence in an object.

Another way to handle multiple sequence-valued parameters is by using keyword parameters:

 Person(name: "John", sons: ("Ed", "John jr"), daughters: "Betty")

This returns a Person object, passing in a sequence of 2 sons, and a different sequence of 1 daughter. This could be defined:

 function Person(name: n, sons: s default (), daughters: d default ())
 { ... }

Note this latter is actually a special case of wrapping an inner sequence in an object, because a keyword parameter:

 name: value

is equivalent to:

 MapEntry("name", value)

which leads to the relationship between sequences, maps, and objects, which is another discussion.

Back to OpenJFX Compiler.

Also on Fandom

Random Wiki