www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ref type versus ptr type on input

reply "Jonathan" <jdgall84 gmail.com> writes:
If I want to write a function that operates on a struct

struct S { }

What are the differences between:

void(S* s)

void(ref S s)

Also, for my general knowledge, is there a way to set default 
function parameters, such as scope or lazy?
Dec 29 2013
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Sunday, 29 December 2013 at 19:42:39 UTC, Jonathan wrote:
 If I want to write a function that operates on a struct

 struct S { }

 What are the differences between:

 void(S* s)
s is a pointer to an instance of S, in the raw C sense.
 void(ref S s)
s can be used as a normal S, but changes are visible to the caller. It can be imagined as a pointer behind the scenes.
 Also, for my general knowledge, is there a way to set default 
 function parameters, such as scope or lazy?
I don't fully understand what you mean by this.
Dec 29 2013
prev sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 29 December 2013 at 19:42:39 UTC, Jonathan wrote:
 If I want to write a function that operates on a struct

 struct S { }

 What are the differences between:

 void(S* s)

 void(ref S s)
You cannot set a default value (like null) for ref parameters, but you can for pointer.
Dec 29 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 29 December 2013 at 21:47:52 UTC, Namespace wrote:
 On Sunday, 29 December 2013 at 19:42:39 UTC, Jonathan wrote:
 If I want to write a function that operates on a struct

 struct S { }

 What are the differences between:

 void(S* s)

 void(ref S s)
You cannot set a default value (like null) for ref parameters, but you can for pointer.
Well, a ref *can't* be null. In terms of default value, both can have them, in terms of referencing a static object though. The only functional difference I know of in pass-by-ref vs pass-by-pointer is indeed the null pointer: - Pass by pointer means you *can* pass a null (yay). - But pass by ref means the implementation does not have to worry about null references (yay).
Dec 29 2013
parent "anonymous" <anonymous example.com> writes:
On Sunday, 29 December 2013 at 22:11:22 UTC, monarch_dodra wrote:
 On Sunday, 29 December 2013 at 21:47:52 UTC, Namespace wrote:
 Well, a ref *can't* be null. In terms of default value, both 
 can have them, in terms of referencing a static object though.

 The only functional difference I know of in pass-by-ref vs 
 pass-by-pointer is indeed the null pointer:
 - Pass by pointer means you *can* pass a null (yay).
 - But pass by ref means the implementation does not have to 
 worry about null references (yay).
bool isNullRef(ref int i) { return &i is null; } void main() { int* np = null; assert(isNullRef(*np)); }
Dec 29 2013