<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Jan 7, 2017 at 5:42 PM, Bart <span dir="ltr"><<a href="mailto:bartjunk64@gmail.com" target="_blank">bartjunk64@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
procedure X(var Y);<br>
begin<br>
PInteger(Y)^ := 3; //syntax may be wrong, too lazy to test<br>
end;<br>
<br>
To me this completely defies the purpose of an untyped var parameter,<br>
since in this exmaple you do know, @design time, the type of Y, so why<br>
not write then<br>
<br>
procedure X(var Y: Integer);<br>
begin<br>
Y := 3;<br>
end;<br></blockquote><div><br></div><div>Think about something like a var-type variable, where the actual type of Y might vary.</div><div><br></div><div>procedure XX(var Y; YType: integer);</div><div>begin</div><div> case YType of</div><div> T_INT: PInteger(@Y)^:= 3;</div><div> T_SINGLE: PSingle(@Y)^:=3;</div><div> T_DOUBLE: PDouble(@Y)^:=3;</div><div> end;<br></div><div>end; </div><div><br></div><div>Again, earlier in the thread I gave a link to anySort() example, that could sort any array-like structure (as long as elements of the structure are placed in sequential order in memory).</div><div><br></div><div>As I said mentioned earlier. Untyped parameter, is an implicit pointer (since the value is always passed by reference).</div><div>But there's a compile-time difference.</div><div>If you'd declare a parameter as a pointer </div><div>procedure X( Y:Pointer);</div><div><br></div><div>Then it would be possible to use the function as following:</div><div>X(nil)</div><div>which requires you, at least add additional sanity check into X procedure.</div><div><br></div><div>But if you'd declare it as X(var Y), then the compiler would not allow to pass nil, always requiring a user to provide a valid variable to be passed and writing something like X(nil) is no longer possible.</div><div><br></div><div>Here's an example:</div><div><a href="http://www.freepascal.org/docs-html/rtl/system/iunknown.queryinterface.html">http://www.freepascal.org/docs-html/rtl/system/iunknown.queryinterface.html</a><br></div><div><br></div><div>QueryInterface must return an interface variable. BUT: </div><div>* the actual returned interface type is unknown at compile time.</div><div>* "obj" parameter must not be nil, since the reference must be written somewhere.</div><div><br></div><div>It's also more convenient from syntax point of view, since a developer doesn't have to write "@" symbol when passing a parameter. The reference would be resolved by the compiler itself.</div><div><br></div><div><br></div><div>thanks,<br></div><div>Dmitry</div></div></div></div>