What type are parameters without type like in the class TStringStream:
function Read(var Buffer; Count: Longint): Longint; override;
What is the type of Buffer parameter (is it a type of Pointer ?).
An untyped expression refers to the usage of a parameter marker which is specified without a target data type associated with it, a null value which is specified without a target data type associated with it, or a DEFAULT keyword.
In Delphi to pass by reference you explicitly add the var keyword: procedure myFunc(var mytext:String); This means that myFunc can modify the contents of the string and have the caller see the changes.
Parameters are special kind of variables declared in a subroutine that are used to input some value into that subroutine. And the values that we pass in a function call is called as Arguments. in Delphi we can declare functions/procedures with parameters like following. Example of a Function declaration.
Declaring Procedures and Functions. When you declare a procedure or function, you specify its name, the number and type of parameters it takes, and, in the case of a function, the type of its return value; this part of the declaration is sometimes called the prototype, heading, or header.
I wrote an article about this very topic a few years ago:
What is an untyped parameter?
Untyped parameters are used in a few situations; the TStream.Read
method you ask about most closely matches with the Move
procedure I wrote about. Here's an excerpt:
procedure Move(const Source; var Dest; Count: Integer);
The
Move
procedure copies data from an arbitrary variable into any other variable. It needs to accept sources and destinations of all types, which means it cannot require any single type. The procedure does not modify the value of the variable passed forSource
, so that parameter’s declaration usesconst
instead ofvar
, which is the more common modifier for untyped parameters.
In the case of TStream.Read
, the source is the stream's contents, so you don't pass that in as a parameter, but the destination is the Buffer
parameter shown in the question. You can pass any variable type you want for that parameter, but that means you need to be careful. It's your job, not the compiler's, to ensure that the contents of the stream really are a valid value for the type of parameter you provide.
Read the rest of my article for more situations where Delphi uses untyped parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With