www.digitalmars.com         C & C++   DMDScript  

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


          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

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

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