digitalmars.D.learn - references and function calls
- Neal Becker (7/7) Mar 16 2007 I've been reading the D specification. A couple of questions:
- Daniel Keep (9/16) Mar 16 2007 That's also correct.
- Frits van Bommel (21/28) Mar 16 2007 By default:
- Daniel Keep (10/10) Mar 16 2007 If you check the message source, we actually have the exact same posting
- Neal Becker (6/41) Mar 16 2007 I've found one of the biggest sources of complication of template
- Derek Parnell (70/77) Mar 16 2007 That is correct. There are alternative forms for the various uses for
- Frits van Bommel (8/33) Mar 16 2007 You seem to have a misunderstanding on the semantics of 'out':
- Derek Parnell (10/11) Mar 16 2007 No, I didn't misunderstand. What happened was I wrote quicker than I
I've been reading the D specification. A couple of questions: 1. Do I understand correctly that D has no reference type declarations (there is no e.b., int& x;)? 2. Reading the section on functions, unfortunately I don't think it is clear on how arguments are passed. Do I understand correctly that argument passing is like java: POD are passed by value but other objects (class instances) are passed by reference?
Mar 16 2007
Neal Becker wrote:I've been reading the D specification. A couple of questions: 1. Do I understand correctly that D has no reference type declarations (there is no e.b., int& x;)?That's correct.2. Reading the section on functions, unfortunately I don't think it is clear on how arguments are passed. Do I understand correctly that argument passing is like java: POD are passed by value but other objects (class instances) are passed by reference?That's also correct. -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 16 2007
Neal Becker wrote:I've been reading the D specification. A couple of questions: 1. Do I understand correctly that D has no reference type declarations (there is no e.b., int& x;)?Yes. (but see below for parameters)2. Reading the section on functions, unfortunately I don't think it is clear on how arguments are passed. Do I understand correctly that argument passing is like java: POD are passed by value but other objects (class instances) are passed by reference?By default: Everything but objects and arrays are passed by value. Objects are passed by reference (or perhaps more correctly: the reference to the data is passed by value). Arrays are similar: The data in the array is passed by reference, but the reference to it is passed by value. In the case of dynamic arrays, the reference also contains the length so this makes a difference. But this behavior can be changed by specifying 'inout' or 'out'. Both of those tell the compiler the parameter is to be passed by reference. In the case of objects and arrays, the reference is passed by reference. That means you can change the object or data being referred to and/or the length of dynamic arrays. The difference between 'out' and 'inout' is that 'out' default-initializes the parameter at the start of the function, so you can't pass in data that way. You can also specify 'in', but that specifies the default behavior. There's also 'lazy', which turns the expression passed into a delegate that evaluates the expression. The expression is only evaluated if the delegate is ever called, but it's re-evaluated every time it's called.
Mar 16 2007
If you check the message source, we actually have the exact same posting timestamp (not Date from the point of origin--the server's NNTP-Posting-Date) :P Jinx! -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 16 2007
Frits van Bommel wrote:Neal Becker wrote:I've found one of the biggest sources of complication of template programming in c++ was the variety of parameters: T, T& T const& I'm wondering if D lack of reference types will simplify things here, or do the same issues just show up in a different guise?I've been reading the D specification. A couple of questions: 1. Do I understand correctly that D has no reference type declarations (there is no e.b., int& x;)?Yes. (but see below for parameters)2. Reading the section on functions, unfortunately I don't think it is clear on how arguments are passed. Do I understand correctly that argument passing is like java: POD are passed by value but other objects (class instances) are passed by reference?By default: Everything but objects and arrays are passed by value. Objects are passed by reference (or perhaps more correctly: the reference to the data is passed by value). Arrays are similar: The data in the array is passed by reference, but the reference to it is passed by value. In the case of dynamic arrays, the reference also contains the length so this makes a difference. But this behavior can be changed by specifying 'inout' or 'out'. Both of those tell the compiler the parameter is to be passed by reference. In the case of objects and arrays, the reference is passed by reference. That means you can change the object or data being referred to and/or the length of dynamic arrays. The difference between 'out' and 'inout' is that 'out' default-initializes the parameter at the start of the function, so you can't pass in data that way. You can also specify 'in', but that specifies the default behavior. There's also 'lazy', which turns the expression passed into a delegate that evaluates the expression. The expression is only evaluated if the delegate is ever called, but it's re-evaluated every time it's called.
Mar 16 2007
On Fri, 16 Mar 2007 06:54:00 -0400, Neal Becker wrote:I've been reading the D specification. A couple of questions: 1. Do I understand correctly that D has no reference type declarations (there is no e.b., int& x;)?That is correct. There are alternative forms for the various uses for references to data. For example, the use of inout and out in function signatures.2. Reading the section on functions, unfortunately I don't think it is clear on how arguments are passed. Do I understand correctly that argument passing is like java: POD are passed by value but other objects (class instances) are passed by reference?Class objects and arrays are passed use a reference mechanism. POD, is passed as copied data if the argument is an 'in' type otherwise it is passed by reference. void funcOne(in int A, inout int B, out int C) { if (A > 0) // The value is readable. A = 1; // The value is changable but 'in' prevents changes // going back to the caller. if (B > 0) // The value is readable. B = 2; // The value is changable and changes // go back to the caller. if (C > 0) // The value is not readable as 'out' always // initializes it before the function gets control. C = 3; // The value is changable and changes // go back to the caller. } int a = 7; int b = 8; int c = 9; funcOne( a,b,c) // Now a is still 7 ('in' prevented changes.) // b is 2 (change got back to me) // c is still 9 ('out' prevented called func from seeing // its value at call time) void funcTwo(in char[] A, inout char[] B, out char[] C) { // NB: When passing arrays, the value passed is the reference! if (A.length > 0) // The value is readable. A = "a".dup; // The value is changable but 'in' //prevents changes // going back to the caller. if (B.length > 0) // The value is readable. B = "b".dup; // The value is changable and changes // go back to the caller. if (C.length > 0) // The value is not readable as 'out' always // initializes it before the function gets control. C = "c".dup; // The value is changable and changes // go back to the caller. } char[] aa = "aaa".dup; char[] bb = "bbb".dup; char[] cc = "ccc".dup; funcTwo( a,b,c) // Now aa is still "aaa" ('in' prevented changes.) // bb is "b" (change got back to me) // cc is still "ccc" ('out' prevented called func from seeing // its value at call time) Also note that passing arrays as 'in' or 'inout' both permit the called function to change the data referenced by the parameter. void funcThree(in char[] a) { if (a.length > 0) a[0] = 'A'; // 'in' only prevents changes to 'a' and // not changes to what 'a' refers to. // Thus changes I make to the .ptr or .length // properties of 'a' are not sent back to the // caller. } char[] A = "321".dup; funcThree(A); // The data in the array is now "A21" -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 16 2007
You seem to have a misunderstanding on the semantics of 'out': Derek Parnell wrote: [removed irrelevant lines from below source]void funcOne(in int A, inout int B, out int C) { if (C > 0) // The value is not readable as 'out' always // initializes it before the function gets control. C = 3; // The value is changable and changes // go back to the caller. } int c = 9; funcOne( a,b,c) // c is still 9 ('out' prevented called func from seeing // its value at call time)Wrong. c is now 0, because 'out' parameters are initialized at the start of the function. When the condition was evaluated c == C == 0 so the condition is false and c remains 0, *even after the function returns*.void funcTwo(in char[] A, inout char[] B, out char[] C) { // NB: When passing arrays, the value passed is the reference! if (C.length > 0) // The value is not readable as 'out' always // initializes it before the function gets control. C = "c".dup; // The value is changable and changes // go back to the caller. } char[] cc = "ccc".dup; funcTwo( a,b,c) // cc is still "ccc" ('out' prevented called func from seeing // its value at call time)cc should now be null (.length == 0, .ptr == null) for similar reasons as in the first example.
Mar 16 2007
On Fri, 16 Mar 2007 14:27:14 +0100, Frits van Bommel wrote:You seem to have a misunderstanding on the semantics of 'out':No, I didn't misunderstand. What happened was I wrote quicker than I thought, and then I didn't review my writing before posting. You are correct that the result of 'out' parameters is an initialized value if the called function doesn't do anything to it. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Mar 16 2007