digitalmars.D.learn - ref type versus ptr type on input
- Jonathan (7/7) Dec 29 2013 If I want to write a function that operates on a struct
- John Colvin (5/12) Dec 29 2013 s can be used as a normal S, but changes are visible to the
- Namespace (3/8) Dec 29 2013 You cannot set a default value (like null) for ref parameters,
- monarch_dodra (8/20) Dec 29 2013 Well, a ref *can't* be null. In terms of default value, both can
- anonymous (10/18) Dec 29 2013 bool isNullRef(ref int i)
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
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
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
On Sunday, 29 December 2013 at 21:47:52 UTC, Namespace wrote:On Sunday, 29 December 2013 at 19:42:39 UTC, Jonathan 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).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
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