From slices to perfect imitators: opByValue

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Wed May 7 23:48:57 PDT 2014


Andrei Alexandrescu:

> making user-defined types as powerful as built-in types is a 
> Good Thing(tm).

An example of something useful that I think is not currently easy 
to do with user-defined types (but I think this could be done by 
future built-in tuples):


Tuple!(ref int, bool) foo(ref int x) pure {
     x++;
     return tuple(x, true);
}


> In order to bring about parity, we'd need to
> introduce opByValue which (if present) would be automatically 
> called whenever the object is passed by value into a function.

I suggest to add some usage examples, to help focus the 
discussion.

Currently only the slices decay in mutables, while an immutable 
int doesn't become mutable:

import std.stdio;

void foo(T)(T x) {
     writeln(typeof(x).stringof);
}

void main() {
     immutable a = [1, 2];
     writeln(typeof(a).stringof);
     foo(a);
     immutable y = 10;
     foo(y);
}


Output:

immutable(int[])
immutable(int)[]
immutable(int)

Bye,
bearophile


More information about the Digitalmars-d mailing list