Untyped / typeless parameters in Delphi

lukasz.matuszewski picture lukasz.matuszewski · Dec 18, 2009 · Viewed 8.6k times · Source

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 ?).

Answer

Rob Kennedy picture Rob Kennedy · Dec 18, 2009

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 for Source, so that parameter’s declaration uses const instead of var, 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.