Question

What do the in and out keywords in D actually mean and do? From looking around at functions using these for their parameters I understand that the in keyword is used for function input and the out keyword is used for parameters which are basically passed by reference.

Is that understanding correctly, and what do they actually allow or disallow a programmer to do?

Était-ce utile?

La solution

Yeah, you basically get it. in expands to const scope meaning you cannot change the variable (or anything it points to) and also are not supposed to keep a reference to it anywhere (scope is not actually implemented in most cases though). Basically, in is look, don't touch.

out means the given variable receives a value. It is very similar to ref - changes to it inside the function are also seen on the outside - with the small difference that out variables are initialized to their normal init value, clearing the value they had before the function was called.

Basically, void foo(out int a) {} == void foo(ref int a) { a = 0; /* inserted automatically */ }

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top