D - Pass by Reference? Pass by Value?
- Kris (8/8) Apr 21 2004 I must have missed this in the documentation somewhere ... what is the
- J Anderson (7/15) Apr 21 2004 Default which is "in" works like a copy (by value) . Now *supposedly D
- C. Sauls (8/8) Apr 21 2004 As J said, the default (in) is always pass-by-value (although it behaves...
- Kris (19/27) Apr 21 2004 Thanks guys.
- J Anderson (38/71) Apr 21 2004 I posted this before, may be helpful (although I got no response).
- Kris (10/94) Apr 21 2004 Forgive me Joel; I don't see a "const" keyword in your example, so I don...
- Kris (11/118) Apr 21 2004 Yep - I was asleep. Thanks for the suggestion.
- J Anderson (5/7) Apr 21 2004 I guess this example was to do with arrays. But you could easily change...
- Kris (32/40) Apr 21 2004 It was actually with structs. See the setStatus() method below.
I must have missed this in the documentation somewhere ... what is the default argument passing convention for structs and classes? Presumably they are both by reference? I ask because it appears as though some of my structs are being passed by value, and I can't change the argument type to 'inout' because the struct is statically declared as a const ('inout' causes a compile error). Any help would be much appreciated. - Kris
Apr 21 2004
Kris wrote:I must have missed this in the documentation somewhere ... what is the default argument passing convention for structs and classes? Presumably they are both by reference? I ask because it appears as though some of my structs are being passed by value, and I can't change the argument type to 'inout' because the struct is statically declared as a const ('inout' causes a compile error). Any help would be much appreciated. - KrisDefault which is "in" works like a copy (by value) . Now *supposedly D will optimise the code if no copy is really needed*. For classes since they are really a pointer to a class the pointer is passed, so changes made to that class will effect the passed in one. -- -Anderson: http://badmama.com.au/~anderson/
Apr 21 2004
As J said, the default (in) is always pass-by-value (although it behaves like reference for classes, because of how they're stored). You can always use a pointer... yes yes I know. The function code would really work identically (since D does away with '->' syntax in favor of '.') but the caller would have to toss in the darn '&'... it'd work though. Or at least should. -C. Sauls -Invironz
Apr 21 2004
Thanks guys. This identifies a syntactic weakness in D then, as Walter alluded to when he wrote "It (perhaps?) eliminates the need for reference (&) declarations." vis-a-vis 'out/inout' in the reference documentation. One should presumably strive to avoid using pointers as arguments (unless there's particular reason for doing so), so one would then move on to using 'inout' instead to force a call-by-reference. By doing so, the user is not forced into thinking about '&' et. al. and all is indeed sweetness and light. However, you can't pass a struct as an 'inout' argument if it is declared const, for obvious reasons. Therefore you either have to remove the const declaration from the struct, or live with the pushing gobs of unnecessary data onto the stack. Neither of these are appropriate long-term solutions. This is hardly a showstopper, but it would be good to get it onto the list for post-v1.0 fixes ... Ideas? Walter? - Kris "C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c65ick$6ul$1 digitaldaemon.com...As J said, the default (in) is always pass-by-value (although it behaves like reference for classes, because of how they're stored). You can always use a pointer... yes yes I know. The function code would really work identically (since D does away with '->' syntax in favor of '.') but the caller would have to toss in the darn '&'... it'd work though. Or at least should. -C. Sauls -Invironz
Apr 21 2004
Kris wrote:Thanks guys. This identifies a syntactic weakness in D then, as Walter alluded to when he wrote "It (perhaps?) eliminates the need for reference (&) declarations." vis-a-vis 'out/inout' in the reference documentation. One should presumably strive to avoid using pointers as arguments (unless there's particular reason for doing so), so one would then move on to using 'inout' instead to force a call-by-reference. By doing so, the user is not forced into thinking about '&' et. al. and all is indeed sweetness and light. However, you can't pass a struct as an 'inout' argument if it is declared const, for obvious reasons. Therefore you either have to remove the const declaration from the struct, or live with the pushing gobs of unnecessary data onto the stack. Neither of these are appropriate long-term solutions. This is hardly a showstopper, but it would be good to get it onto the list for post-v1.0 fixes ... Ideas? Walter? - KrisI posted this before, may be helpful (although I got no response). //module 1 template constArray(T) //This would be in another module { struct Array { private T [] array; static Array opCall(T [] array) { Array t; t.array = array; return t; } T opIndex(int i) { return array[i]; } int length() { return array.length; } } } alias constArray!(int).Array constA; //module 2 void test(constA array) { int x = array[0]; //You just can't change the array variable (well at least not without force). } void main() { int [] a; a ~= 10; a ~= 30; test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing.... } -- -Anderson: http://badmama.com.au/~anderson/"C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c65ick$6ul$1 digitaldaemon.com...-- -Anderson: http://badmama.com.au/~anderson/As J said, the default (in) is always pass-by-value (although it behaves like reference for classes, because of how they're stored). You can always use a pointer... yes yes I know. The function code would really work identically (since D does away with '->' syntax in favor of '.') but the caller would have to toss in the darn '&'... it'd work though. Or at least should. -C. Sauls -Invironz
Apr 21 2004
Forgive me Joel; I don't see a "const" keyword in your example, so I don't see any kind of workaround. Perhaps I'm still asleep ... - Kris "J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:c66edq$1o48$1 digitaldaemon.com...Kris wrote:heThanks guys. This identifies a syntactic weakness in D then, as Walter alluded to whenusingwrote "It (perhaps?) eliminates the need for reference (&) declarations." vis-a-vis 'out/inout' in the reference documentation. One should presumably strive to avoid using pointers as arguments (unless there's particular reason for doing so), so one would then move on tonot'inout' instead to force a call-by-reference. By doing so, the user issolutions.forced into thinking about '&' et. al. and all is indeed sweetness and light. However, you can't pass a struct as an 'inout' argument if it is declared const, for obvious reasons. Therefore you either have to remove the const declaration from the struct, or live with the pushing gobs of unnecessary data onto the stack. Neither of these are appropriate long-termlistThis is hardly a showstopper, but it would be good to get it onto thefor post-v1.0 fixes ... Ideas? Walter? - KrisI posted this before, may be helpful (although I got no response). //module 1 template constArray(T) //This would be in another module { struct Array { private T [] array; static Array opCall(T [] array) { Array t; t.array = array; return t; } T opIndex(int i) { return array[i]; } int length() { return array.length; } } } alias constArray!(int).Array constA; //module 2 void test(constA array) { int x = array[0]; //You just can't change the array variable (well at least not without force). } void main() { int [] a; a ~= 10; a ~= 30; test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing.... } -- -Anderson: http://badmama.com.au/~anderson/"C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c65ick$6ul$1 digitaldaemon.com...-- -Anderson: http://badmama.com.au/~anderson/As J said, the default (in) is always pass-by-value (although it behaves like reference for classes, because of how they're stored). You can always use a pointer... yes yes I know. The function code would really work identically (since D does away with '->' syntax in favor of '.') but the caller would have to toss in the darn '&'... it'd work though. Or at least should. -C. Sauls -Invironz
Apr 21 2004
Yep - I was asleep. Thanks for the suggestion. "Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message news:c66f83$1qhf$1 digitaldaemon.com...Forgive me Joel; I don't see a "const" keyword in your example, so I don't see any kind of workaround. Perhaps I'm still asleep ... - Kris "J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:c66edq$1o48$1 digitaldaemon.com...whenKris wrote:Thanks guys. This identifies a syntactic weakness in D then, as Walter alluded tohedeclarations."wrote "It (perhaps?) eliminates the need for reference (&)(unlessvis-a-vis 'out/inout' in the reference documentation. One should presumably strive to avoid using pointers as argumentsdeclaredusingthere's particular reason for doing so), so one would then move on tonot'inout' instead to force a call-by-reference. By doing so, the user isforced into thinking about '&' et. al. and all is indeed sweetness and light. However, you can't pass a struct as an 'inout' argument if it isconstconst, for obvious reasons. Therefore you either have to remove theunnecessarydeclaration from the struct, or live with the pushing gobs ofbehavessolutions.data onto the stack. Neither of these are appropriate long-termlistThis is hardly a showstopper, but it would be good to get it onto thefor post-v1.0 fixes ... Ideas? Walter? - KrisI posted this before, may be helpful (although I got no response). //module 1 template constArray(T) //This would be in another module { struct Array { private T [] array; static Array opCall(T [] array) { Array t; t.array = array; return t; } T opIndex(int i) { return array[i]; } int length() { return array.length; } } } alias constArray!(int).Array constA; //module 2 void test(constA array) { int x = array[0]; //You just can't change the array variable (well at least not without force). } void main() { int [] a; a ~= 10; a ~= 30; test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing.... } -- -Anderson: http://badmama.com.au/~anderson/"C. Sauls" <ibisbasenji yahoo.com> wrote in message news:c65ick$6ul$1 digitaldaemon.com...As J said, the default (in) is always pass-by-value (although itreallylike reference for classes, because of how they're stored). You can always use a pointer... yes yes I know. The function code would-- -Anderson: http://badmama.com.au/~anderson/work identically (since D does away with '->' syntax in favor of '.') but the caller would have to toss in the darn '&'... it'd work though. Or at least should. -C. Sauls -Invironz
Apr 21 2004
Kris wrote:Yep - I was asleep. Thanks for the suggestion.I guess this example was to do with arrays. But you could easily change it for classes (although probably not as neat). -- -Anderson: http://badmama.com.au/~anderson/
Apr 21 2004
It was actually with structs. See the setStatus() method below. // Status is a compound type, with a name and a code. struct HttpStatus { int code; char[] name; } // traditional response codes enum HttpResponseCode { Continue = 100, SwitchingProtocols = 101, OK = 200, // etc ... } //Declare the traditional set of HTTP responses struct HttpResponses { // these should all be const ... static const HttpStatus Continue = {HttpResponseCode.Continue, "Continue"}; static const HttpStatus OK = {HttpResponseCode.OK, "OK"}; // etc ... } // Argument status cannot be passed by inout-reference, cos' it's const void setStatus (HttpStatus status) { this.status = status; } - Kris "J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:c66pam$2cab$3 digitaldaemon.com...Kris wrote:Yep - I was asleep. Thanks for the suggestion.I guess this example was to do with arrays. But you could easily change it for classes (although probably not as neat). -- -Anderson: http://badmama.com.au/~anderson/
Apr 21 2004