www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22396] New: Assignments from function value parameters should

https://issues.dlang.org/show_bug.cgi?id=22396

          Issue ID: 22396
           Summary: Assignments from function value parameters should pass
                    by move when possible
           Product: D
           Version: D2
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: per.nordlow gmail.com

The number of cases where a variable (including function parameters passed by
value) is be passed by move should be extend to include at least assignments if
that assignment statement is the last reference of the variable.

This enables non-copyable (r-value) ranges to be passed to common range-based
Phobos algorithms. Such r-value ranges are typically created when a
non-copyable C++/Rust-style containers is passed by move _into_ a specific
range I propose to name `UniqueInputRange`. `UniqueInputRange`, itself
non-copyable, can then with this change be passed to Phobos' higher-order
algorithms, typically `map`, `filter`, `reduce` etc.

This idea has already be realized in Rust using
[IntoIterator](https://doc.rust-lang.org/std/iter/trait.IntoIterator.html).

Specifically, range algorithm constructor should be able to take a non-copyable
range as its source range parameter as in

```d
struct SomeRange
{
    this(R range)
    {
        source = range; // TODO `range` should be passed by move to `source`
    }
}
```

when `range` is not referenced after it being moved in the assignment. If the
compiler is modified to automatically allow such assignments to perform move
all the details described above can be implemented in phobos.

See also https://github.com/dlang/phobos/pull/4971.

--
Oct 16 2021