Language Idea #6892: in array ops, enable mixing slices and random access ranges
Guillaume Piolat
notthat at email.com
Mon Feb 5 17:35:45 UTC 2018
General idea
============
Currently arrays ops express loops over slices.
a[] = b[] * 2 + c[]
It would be nice if one could mix a random access range into such
an expression.
The compiler would have builtin support for random access range.
Example
=======
------------------------------>3---------------------------------------
import std.algorithm;
import std.array;
import std.range;
void main()
{
int[] A = [1, 2, 3];
// arrays ops only work with slices
A[] += iota(3).array[];
// Check that iota is a random access range
auto myRange = iota(3);
static assert( isRandomAccessRange!(typeof(myRange)) );
// Doesn't work, array ops can't mix random access ranges and
slices
// NEW
A[] += myRange[]; // whatever syntax could help the compiler
}
------------------------------>3---------------------------------------
How it could work
=================
A[] += myRange[]; // or another syntax for "myRange as an
array op operand"
would be rewritten to:
foreach(i; 0..A.length)
A[i] += myRange[i];
myRange should not be a range without "length".
Why?
====
Bridges a gap between lazy generation and array ops, now that
array ops are reliable.
Allow arrays ops to take slice-like objects.
What do you think?
More information about the Digitalmars-d
mailing list