Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the 'in' and 'out' keywords mean in D?

Tags:

d

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?

like image 334
Jeroen Avatar asked Feb 14 '23 17:02

Jeroen


1 Answers

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 */ }

like image 194
Adam D. Ruppe Avatar answered Feb 24 '23 14:02

Adam D. Ruppe