www.digitalmars.com         C & C++   DMDScript  

D - in/out/inout questions

reply brad beveridge <brad nospam.com> writes:
Hi there, I've just started playing with D & am wondering about the 
in/out/inout parameters.
Firstly - the documentation example of
	void foo(out int bar)
	{
	}

	int bar = 3;
	foo(bar);
	// bar is now 0
doesn't work :)  bar is still 3.
so for "out" bar isn't being initialised to 0.

Next, because objects are passed by reference, is there anyway to make 
them "const"?  Or is that an assertation using the design by contract?

Cheers
Brad
Nov 30 2003
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
brad beveridge wrote:
 Hi there, I've just started playing with D & am wondering about the 
 in/out/inout parameters.
 Firstly - the documentation example of
     void foo(out int bar)
     {
     }
 
     int bar = 3;
     foo(bar);
     // bar is now 0
 doesn't work :)  bar is still 3.
 so for "out" bar isn't being initialised to 0.

Although I see your point, I think that the current implementation makes sense. If the function does nothing, then really nothing should be done. I could see how people might argue otherwise, but it's really a coin toss what to do, IMHO.
 Next, because objects are passed by reference, is there anyway to make 
 them "const"?  Or is that an assertation using the design by contract?

Walter has stated that he won't support const in function parameters because they are too problematic. They force a lot of weird rules on the language, apparently. (I don't remember his posts in enough detail to remember exactly what scenarios are problematic.) 'const', in D, is used only for global constants. It is a clue to the compiler that these variables are good ones to locate in the read-only section of the linked executable. (Thus, an attempt to write to them should cause a segmentation fault.)
Nov 30 2003