Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-03

Mode Delphi and functions as parameters.

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

vfclists .

I have recently compiled projects which were converted from Delphi and it seems to me that the passing a function as a parameter does not require the '@' symbol. I suspect syntax errors came because I wasn't compiling from the command line.

e.g

function AFunction()
begin
end;

CallFunctionWithFunctionParameter (AFunction) instead of CallFunctionWithFunctionParameter (@AFunction)

Is passing a function as parameter without the '@' symbol accepted in Delphi?

Frank Church ======================= http://devblog.brahmancreations.com
Michael Van Canneyt
to vfclists .
vfclists . wrote 14 lines
I have recently compiled projects which were converted from Delphi and it seems to me that the passing a function as a parameter does not require the '@' symbol. I suspect syntax errors came because I wasn't compiling from the command line. e.g function AFunction() begin end; CallFunctionWithFunctionParameter (AFunction) instead of CallFunctionWithFunctionParameter (@AFunction) Is passing a function as parameter without the '@' symbol accepted in Delphi?

If you use Delphi mode: yes.

Michael.

Constantine Yannakopoulos
to Michael Schnell
Michael Schnell wrote
what to to if a function has no parameter and returns a value that is a pointer to a function of exactly this type ?

​The logical thing for the compiler would be to assume that the programmer meant that a reference should be passed​. If the programmer wanted to invoke the function ans pass the result reference they should use empty parentheses aka "the invocation operator":

CallFunction(SomeFunction); // passes a reference to SomeFunction.
CallFunction(SomeFunction()); // passes the result of the invocation of
SomeFunction.

​So no ambiguity, albeit a very subtle semantic difference that can easily produce bugs.​ But IMHO the "@" notation isn't much better either.

PS: I tend to always use the "()" notation when invoking functions with no arguments so it will be clear to a future reader that I'm invoking a function and not assigning some variable. Makes a difference with poorly named functions (not starting with a verb):

ADate := Date;
ADate := Date();
Jürgen Hestermann
to Constantine Yannakopoulos
Constantine Yannakopoulos wrote
PS: I tend to always use the "()" notation when invoking functions with no arguments so it will be clear to a future reader that I'm invoking a function and not assigning some variable.

This should be the job of the langugage definition, not of the programmer: Avoid ambiguity, do not base anything on assumptions. That avoids bugs by making it very clear to the programmer what realy happens.

Saving one or 2 key keystrokes is of no use.
A clear language definition is what makes the essence of a programming language.

vfclists .
to Constantine Yannakopoulos: On Fri, Mar 20, 2015 at 11:55 AM, Michael Schnell ​The logical thing for the compiler…
Constantine Yannakopoulos wrote 32 lines
On Fri, Mar 20, 2015 at 11:55 AM, Michael Schnell wrote: > what to to if a function has no parameter and returns a value that is a > pointer to a function of exactly this type ? ​The logical thing for the compiler would be to assume that the programmer meant that a reference should be passed​. If the programmer wanted to invoke the function ans pass the result reference they should use empty parentheses aka "the invocation operator": CallFunction(SomeFunction); // passes a reference to SomeFunction. CallFunction(SomeFunction()); // passes the result of the invocation of SomeFunction. ​So no ambiguity, albeit a very subtle semantic difference that can easily produce bugs.​ But IMHO the "@" notation isn't much better either. PS: I tend to always use the "()" notation when invoking functions with no arguments so it will be clear to a future reader that I'm invoking a function and not assigning some variable. Makes a difference with poorly named functions (not starting with a verb): ADate := Date; ADate := Date(); _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Compilers should not assume. Programmers should be explicit about what they want.
Assumptions based on ambiguous semantics are an unending source of problems.

Frank Church ======================= http://devblog.brahmancreations.com
vfclists .
to vfclists .: I have recently compiled projects which were converted from Delphi and it seems to me…
vfclists . wrote 25 lines
I have recently compiled projects which were converted from Delphi and it seems to me that the passing a function as a parameter does not require the '@' symbol. I suspect syntax errors came because I wasn't compiling from the command line. e.g function AFunction() begin end; CallFunctionWithFunctionParameter (AFunction) instead of CallFunctionWithFunctionParameter (@AFunction) Is passing a function as parameter without the '@' symbol accepted in Delphi? -- Frank Church ======================= http://devblog.brahmancreations.com

More questions on Mode Delphi.

1. Does Mode Delphi simply allow Delphi syntax to be compiled, or does it also affect the code generation, like data structures, pointer handling, parameter handling / calling conventions, stack frames etc?

2. When Mode Delphi is applied to a project, ie the .dpr, does it affect the whole project, or does each project require its own mode delphi statement?

3. Can it be switched on and off within the same unit, ie some procedures and data structures use it, some don't?

Frank Church ======================= http://devblog.brahmancreations.com
Mattias Gaertner
to vfclists .
Show quoted text 6 lines
[...] More questions on Mode Delphi. 1. Does Mode Delphi simply allow Delphi syntax to be compiled, or does it also affect the code generation, like data structures, pointer handling, parameter handling / calling conventions, stack frames etc?

Compared to mode objfpc: no.
Other modes might have other aligns and calling conventions. But you can change that with compiler directives, so basically a mode is just a nice set of start switches.

2. When Mode Delphi is applied to a project, ie the .dpr, does it affect the whole project, or does each project require its own mode delphi statement?

The command line parameters are applied to the whole project, the compiler directives only to the unit.

3. Can it be switched on and off within the same unit, ie some procedures and data structures use it, some don't?

See compiler directives.

Mattias