Skip to content

Commit d5038c3

Browse files
authored
[spec/arrays] Improve vector operations docs (#3654)
* [spec/arrays] Improve vector operations docs Change title to Vector Operations, array operations is not precise enough IMO and hard to google for. Define which operations are supported. Rewrite example & make runnable. Add note warning about invalid expressions for vector op. Fixes Issue 5636 - Array ops use lexicographic comparison instead of vector-style element-wise. * Element type operator restrictions
1 parent 70d212d commit d5038c3

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

spec/arrays.dd

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ a ~= b; // a becomes the concatenation of a and b
473473
setting dynamic array length) for details.
474474
)
475475

476-
$(H2 $(LNAME2 array-operations, Array Operations))
476+
$(H2 $(LNAME2 array-operations, Vector Operations))
477477

478-
$(P Many array operations, also known as vector operations,
478+
$(P Many array operations
479479
can be expressed at a high level rather than as a loop.
480480
For example, the loop:
481481
)
@@ -500,28 +500,50 @@ a[] = b[] + 4;
500500

501501
$(P A vector operation is indicated by the slice operator appearing
502502
as the left-hand side of an assignment or an op-assignment expression.
503-
The right-hand side can be an expression consisting either of an array
504-
slice of the same length and type as the left-hand side or a scalar expression
505-
of the element type of the left-hand side, in any combination.
506-
)
503+
The right-hand side can be certain combinations of:)
504+
505+
* An array $(GLINK2 expression, SliceExpression) of the same length
506+
and type as the left-hand side
507+
* A scalar expression of the same element type as the left-hand side
508+
509+
$(P The following operations are supported:)
507510

511+
* Unary: `-`, `~`
512+
* Add: `+`, `-`
513+
* Mul: `*`, `/`, `%`,
514+
* Bitwise: `^`, `&`, `|`
515+
* Pow: `^^`
516+
517+
$(SPEC_RUNNABLE_EXAMPLE_RUN
508518
---
509-
T[] a, b, c;
510-
...
511-
a[] -= (b[] + 4) * c[];
519+
int[3] a = 0;
520+
int[] b = [1, 2, 3];
521+
522+
a[] += 10 - (b[] ^^ 2);
523+
assert(a == [9, 6, 1]);
512524
---
525+
)
526+
527+
$(NOTE In particular, an expression using
528+
$(GLINK2 expression, ConditionalExpression),
529+
$(DDSUBLINK spec/expression, logical_expressions, logical expressions),
530+
$(GLINK2 expression, CmpExpression),
531+
concatenation `~` or a function call is *not* a vector op.)
513532

514533
$(P The slice on the left and any slices on the right must not overlap.
515534
All operands are evaluated exactly once, even if the array slice
516535
has zero elements in it.
517536
)
518537

538+
$(P If the element type defines matching overloaded operators,
539+
those methods must be `pure nothrow @nogc`.)
540+
519541
$(P The order in which the array elements are computed
520542
is implementation defined, and may even occur in parallel.
521543
An application must not depend on this order.
522544
)
523545

524-
$(P Implementation note: Many vector operations are expected
546+
$(P $(B Implementation Note:) Many vector operations are expected
525547
to take advantage of any vector math instructions available on
526548
the target computer.
527549
)

0 commit comments

Comments
 (0)