Skip to content

Commit

Permalink
doc/ref/spec.md: get rid of list operators
Browse files Browse the repository at this point in the history
List operators are confusing. For instance,
is the result of [a, ...] + [b, ...] open or close?
What about [a] + [b, ...]? And so on.

With the current semantics of comprehensions, they
are also unnecessary. The above can be written as
    [ for x in a {x}, for x in b {x} ].

Though more verbose, it is very clear here that
regardless of whether a and b are open or closed
the result is always closed.

With the query extension, this can also be written
more succinctly, like `[ a.[_], b.[_] ]` or perhaps
`[a.*, b.*]`, either case is clearer than using list
operators.

See Issue #165.

In order to move to being able to give backwards
compatiblity guarantees, we propose getting rid of list
addition and multiplication. An automatic rewriter could
rewrite the old use using `list.Repeat` and `list.Concat`,
the latter of which could refer to the spec to indicate
its equivalence to using comprehensions or, later, queries.

Change-Id: I374bfd59775d66d3da9feb28e1940f8bd3c255e8
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8063
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
mpvl committed Jan 9, 2021
1 parent ff48658 commit 172f006
Showing 1 changed file with 3 additions and 36 deletions.
39 changes: 3 additions & 36 deletions doc/ref/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2196,12 +2196,12 @@ x == y+1 && y == z-1
Arithmetic operators apply to numeric values and yield a result of the same type
as the first operand. The four standard arithmetic operators
`(+, -, *, /)` apply to integer and decimal floating-point types;
`+` and `*` also apply to lists and strings.
`+` and `*` also apply to strings and bytes.

```
+ sum integers, floats, lists, strings, bytes
+ sum integers, floats, strings, bytes
- difference integers, floats
* product integers, floats, lists, strings, bytes
* product integers, floats, strings, bytes
/ quotient integers, floats
```

Expand All @@ -2223,39 +2223,6 @@ The unary operators `+` and `-` are defined for numeric values as follows:
-x negation is 0 - x
```

#### List operators

Lists can be concatenated using the `+` operator.
Opens list are closed to their default value beforehand.

```
[ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
[ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ]
[ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4 ]
```

Lists can be multiplied with a non-negative`int` using the `*` operator
to create a repeated the list by the indicated number.
```
3*[1,2] // [1, 2, 1, 2, 1, 2]
3*[1, 2, ...] // [1, 2, 1, 2, 1 ,2]
[byte]*4 // [byte, byte, byte, byte]
0*[1,2] // []
```

<!-- TODO(mpvl): should we allow multiplication with a range?
If so, how does one specify a list with a range of possible lengths?
Suggestion from jba:
Multiplication should distribute over disjunction,
so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x].
The hard part is figuring out what (>=1 & <=3) * [x] means,
since >=1 & <=3 includes many floats.
(mpvl: could constrain arguments to parameter types, but needs to be
done consistently.)
-->


#### String operators

Strings can be concatenated using the `+` operator:
Expand Down

0 comments on commit 172f006

Please sign in to comment.