Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Untyped / typeless parameters in Delphi

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

like image 302
lukasz.matuszewski Avatar asked Dec 18 '09 14:12

lukasz.matuszewski


People also ask

What is an untyped parameter?

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.

How do you pass by reference in Delphi?

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.

What is a parameter Delphi?

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.

How do I declare a function in Delphi?

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.


1 Answers

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.

like image 114
Rob Kennedy Avatar answered Oct 03 '22 22:10

Rob Kennedy