Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 29, 2013 ref type versus ptr type on input | ||||
---|---|---|---|---|
| ||||
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? |
December 29, 2013 Re: ref type versus ptr type on input | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | 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. |
December 29, 2013 Re: ref type versus ptr type on input | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | 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.
|
December 29, 2013 Re: ref type versus ptr type on input | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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).
|
December 29, 2013 Re: ref type versus ptr type on input | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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));
}
|
Copyright © 1999-2021 by the D Language Foundation