Sign in Join

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

Mailing lists / fpc-devel / 2014-02

function overload ?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Gennadiy Poryev

Hi!

I had a dream :)
Wouldn't it be nice if a function overload took care not only of formal parameter list (per docs) but also of result type? So the following may work function My(const a : Integer) : AnsiString; overload; function My(const a : Integer) : WideString; overload; by substitution of the function depending on the expected type of the expression it is being called from?

Reply by email

Michael Van Canneyt
to Gennadiy Poryev
Gennadiy Poryev wrote 9 lines
Hi! I had a dream :) Wouldn't it be nice if a function overload took care not only of formal parameter list (per docs) but also of result type? So the following may work function My(const a : Integer) : AnsiString; overload; function My(const a : Integer) : WideString; overload; by substitution of the function depending on the expected type of the expression it is being called from?

Expressions don't work that way in Pascal.
The types in the expression determine the result type, not the other way round, there is no 'expected type'. That implies that the type of each term in the expression must be known. That in turn means that the compiler cannot decide which function to use based on result type alone.

Michael.

Reply by email

Den
to Gennadiy Poryev: Hi! I had a dream :) Wouldn't it be nice if a function overload took care not only of…

If you're not worried about the slight speed hit and extra space on the stack for the return value of "My" you could always have it as a Variant?

   function My(const a : Integer) : Variant;

Then assign it to whatever variable?

- Dennis Fehr

Gennadiy Poryev wrote 13 lines
Hi! I had a dream :) Wouldn't it be nice if a function overload took care not only of formal parameter list (per docs) but also of result type? So the following may work function My(const a : Integer) : AnsiString; overload; function My(const a : Integer) : WideString; overload; by substitution of the function depending on the expected type of the expression it is being called from? _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email