Can you fix this code to avoid using pointers?

XavierAP via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 11 06:49:42 PST 2017


On Saturday, 11 March 2017 at 13:44:30 UTC, Satoshi wrote:
>
> void calc(in double[] array...) {
> foreach (x; array) { }
> }

To do what I want it should be foreach(ref x; array) -- or const 
ref. But also I don't want to modify the function signature, 
certainly in this way. In another situation yes, but the 
arguments are very different magnitudes, for example 
temperatures, conductivity, heat power, etc. They should be 
separate arguments with self-documenting names. And it's not 
worth the bother to define a struct type for them as a set. 
Specially since this is an internal implementation "problem" that 
shouldn't affect the outer interface.

I know there's something in std.algorithm for this, but afaik it 
would be relatively bloated compared to this pointer solution. In 
C++ I would use a &reference instead of a *pointer, but I 
actually think C++ references are redundant with pointers, not 
much safer, and plain confusing.

I guess it's a not a common case because if a type is non trivial 
to copy it should probably be a class, which is already assigned 
by reference so I wouldn't need the pointer/ref.


More information about the Digitalmars-d-learn mailing list