How to pass the constant "nil" to an untyped parameter?

Is it possible to pass nil as an undeclared constant to the untyped parameter of some function? I have such functions, and I would like to pass some constant to the Data parameter to satisfy the compiler. Inside, I make a decision on the "Size" parameter. I know that I can use Pointer instead of the untyped parameter, but this is much more convenient for my case.

Now I get E2250 There is no overloaded version of 'RS232_SendCommand' that can be called with these arguments

 function RS232_SendCommand(const Command: Integer): Boolean; overload; begin // is it possible to pass here an undeclared constant like nil in this example? Result := RS232_SendCommand(Command, nil, 0); end; function RS232_SendCommand(const Command: Integer; const Data; const Size: Integer): Boolean; overload; begin ... end; 

This works, but I would be glad if I could leave a variable declaration.

 function RS232_SendCommand(const Command: Integer): Boolean; overload; var Nothing: Pointer; begin Result := RS232_SendCommand(Command, Nothing, 0); end; 

The solution is to use something like this.

 function RS232_SendCommand(const Command: Integer): Boolean; overload; begin // as the best way looks for me from the accepted answer to use this Result := RS232_SendCommand(Command, nil^, 0); // or it also possible to pass an empty string constant to the untyped parameter // without declaring any variable Result := RS232_SendCommand(Command, '', 0); end; 

I do this because some of my commands have data sent after passing the commands.

thanks for the help

+7
source share
2 answers

Easy:

 RS232_SendCommand(Command, nil^, 0); 

You just need to make sure that you are not accessing the Data parameter from within RS232_SendCommand , but it is assumed that the size parameter is 0.

In my opinion, this is the best solution, because it clearly states that you are transmitting something that cannot be accessed.

+12
source

No, you can’t. not what i knew about

Untyped parameters

You can omit type specifications when declaring var , const, and out . (Value parameters must be printed.) For Example:

TakeAnything (const C) procedure;

declares a procedure called TakeAnything that takes a parameter of any type. When you name such a routine, you cannot pass a numeric or untyped numeric constant to it.

From: Parameters (Delphi)

So, maybe add another overloaded version without const arg, which you can call when Size = 0.

+2
source

All Articles