[Issue 1869] Semantically returning an array from a funciton is difficult

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 25 15:47:01 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1869





------- Comment #4 from jason at spashett.com  2008-02-25 17:47 -------
(In reply to comment #3)
> Hm. I'm almost sure I've done that before. And no, I use 1.0.
> 
> If my examples don't work, I think it should be considered a bug (in the design
> if not the implementation).
> 
> On seconds though, you point out that a struct can be returned. This will
> actually be done by value (returning a struct with an int[4] inside will copy
> 4*int.sizeof bytes) D might be interpreting returning int[4] in that way and
> refusing (that could get real nasty with some template code computing n=1e6 or
> some such).
> 
> If that is the case, why does it treat it as values on return and as references
> on pass?
> 

D is a bit like java and c# in that small intrinsic items like ints, floats are
passed by value, the same is true of stucts. It's a trade off between
efficiency and pass by value semantics I guess.

An array, and a class are considered largeish things and so it's assumed you
will want them to go 'though' by reference. In fact you can't pass an array by
value even if you use the "in" keyword. In a way arrays work similarly in C and
C++ except in those languages they are pointers and loose type information when
you pass them in. The problem in D I seem to be encountering is that there is
no nice, or convenient way of returning an array (the problem gets worse with
multi dimensional arrays because you have to set them up and until recently in
dmd 2.0 you couldn't initialize them in one go AFIK)

I don't have to return an array, but it makes what I am trying to do a bit more
clumsy. For example I can't pass into (OpenGL) glMatrixMultiply an array of
float[16] by simply calling matrix.toFloat16() I must use a temporary variable,
which is a slight pain. Or; I can use my 'pointer workaround' which is bit of a
hack.


-- 



More information about the Digitalmars-d-bugs mailing list