Thread overview
in out functioning
Feb 17, 2006
nascent
Feb 17, 2006
Chris Sauls
Feb 18, 2006
nascent
February 17, 2006
I can't find good documentation on this like with readf. How exactly should the function(in var, out var2, inout var3) tagging be used? The ones with out are set to the initializer value, but I would think most people like using the values they pass in? So a good explanation of this would be nice, and it doesn't have to be D related if it is found elsewhere, but could someone point me in the right direction?

--Jesse
February 17, 2006
nascent wrote:
> I can't find good documentation on this like with readf. How exactly should the function(in var, out var2, inout var3) tagging be used? The ones with out are set to the initializer value, but I would think most people like using the values they pass in? So a good explanation of this would be nice, and it doesn't have to be D related if it is found elsewhere, but could someone point me in the right direction?
> 
> --Jesse

The 'in' attribute is mostly just there for completeness (for now, some proposals suggest changing it to have a new meaning, but I won't get in to that).  It just represents the normal pass-by-value behavior.

The 'out' attribute re-initializes the variable, and then passes it by reference. Basically, its for passing a buffer.

The 'inout' attribute establishes pass-by-reference without any processing of the variable.  So if you want to use the value passed, /and/ want to be able to edit that value in place, use 'inout'.  If you don't care about the value passed in, use 'out'.  If you don't want the variable's value changed at all, use 'in'.

Of course this doesn't account for slice semantics, and object variables are technically always references -- which means that the 'in' behavior just prevents you from changing /which object/ the variable references.  You can still modify that object.

-- Chris Nicholson-Sauls
February 18, 2006
Thanks, I'll have to try it some time.

Chris Sauls wrote:
> nascent wrote:
> 
>> I can't find good documentation on this like with readf. How exactly should the function(in var, out var2, inout var3) tagging be used? The ones with out are set to the initializer value, but I would think most people like using the values they pass in? So a good explanation of this would be nice, and it doesn't have to be D related if it is found elsewhere, but could someone point me in the right direction?
>>
>> --Jesse
> 
> 
> The 'in' attribute is mostly just there for completeness (for now, some proposals suggest changing it to have a new meaning, but I won't get in to that).  It just represents the normal pass-by-value behavior.
> 
> The 'out' attribute re-initializes the variable, and then passes it by reference. Basically, its for passing a buffer.
> 
> The 'inout' attribute establishes pass-by-reference without any processing of the variable.  So if you want to use the value passed, /and/ want to be able to edit that value in place, use 'inout'.  If you don't care about the value passed in, use 'out'.  If you don't want the variable's value changed at all, use 'in'.
> 
> Of course this doesn't account for slice semantics, and object variables are technically always references -- which means that the 'in' behavior just prevents you from changing /which object/ the variable references.  You can still modify that object.
> 
> -- Chris Nicholson-Sauls