www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - in out functioning

reply nascent <jesse.phillips eagles.ewu.edu> writes:
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
Feb 17 2006
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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
Feb 17 2006
parent nascent <jesse.phillips eagles.ewu.edu> writes:
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

Feb 18 2006