This is done by var already. There is no reason to add a new keyword for something that already exists.
No. It is not done by var already.
var cannot make any assumptions about the state, initialized or otherwise, of the variable being passed. It simply tells the compiler to pass the variable by reference, that's all it does, absolutely nothing else.
var gives no information to the compiler as to the state of the variable, whether it must be initialized or not before using it is passed as a parameter.
out is the same as var with the addition that it informs the compiler that the variable will be
written to in the function/procedure. That additional information enables the compiler to emit a hint or warning if the out parameter isn't being written to. IOW, it helps the programmer catch errors (it's nice to have a compiler's help in catching such easy to overlook details.)
The reason presented for not implementing
inout isn't particularly solid (I'm being kind) because the same reason could have been used for not implementing
out and
constref for instance.
var already does what
out and
constref do (the argument you're presenting) but, in addition to that, they provide additional information to the compiler. In the case of
constref it informs the compiler that the variable, in spite of being passed by reference, is a read-only variable which enables the compiler to emit a warning or an error in the case of
constref, if the programmer tries to write/modify the "constant". It's nice to have the compiler's help to ensure the code doesn't do something that was not intended (which is what constref adds to var.)
inout would make it clear to the compiler that the variable being passed _must_ be initialized before passing it as a parameter. This would enable the compiler to emit a hint/warning if the variable has not been initialized. IOW, the programmer is giving information to the compiler, which the compiler can use to warn the programmer he/she failed to initialize the variable. Again, it's rather nice to have the compiler help the programmer ensure that the code is as it should be (which is why Pascal has strong typing, to help the programmer correct mistakes.)
There are good reasons why declarators such as "in", "out" and "inout" are being "synthesized" in C/C++ header declarations and, it is not because C/C++ programmers like to type.
And just for orthogonality.
constref is a synonym for "in" (passing an uninitialized constref variable is rather likely to be a mistake.)
out is already what its name says it is (the parameter will be modified/written to). What's missing is a way to tell the compiler about parameters that are both
in and
out which
var does not do, thereby letting the compiler know that the variable must be initialized before being used as a parameter _and_ written to in procedure/function. If those two conditions are not met then, the compiler can emit a hint or a warning letting the programmer know that his/her usage of the variable is not as he/she declared it.
There is another reason too: currently the -gt compiler switch incorrectly assumes that an out parameter is not initialized by the caller. out is very clear, it tells the compiler that the variable will be written to in the function/procedure, it says absolutely nothing about the initialization state of the variable being passed to the function/procedure.
inout would provide a way for the compiler to help the programmer write better code and, it would also justify - the currently incorrect - assumption made by the -gt switch.
Do you have an example for that? Cause it's a bit unclear what exactly you mean here...
There is an entire thread on the subject
http://forum.lazarus.freepascal.org/index.php/topic,41857.0.html -gt
assumes that an
out parameter has _not_ been initialized (by simply trashing it upon entry) an assumption which is completely unwarranted and incorrect, not to mention that it's supposed to trash local variables not parameters. At least if there was an
inout keyword, -gt could justify its behavior by noting that the programmer didn't specify the variable was initialized by specifying
inout.