Sign in Join

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

Mailing lists / fpc-pascal / 2014-09

Overloading functions

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Mark Morgan Lloyd

My understanding is that functions cannot be overloaded based on the return type, i.e. you can't have two functions with the same name and parameters which differ only in the return type. Is there any realistic way this can be worked around?

I've been reading the first few (i.e. free :-) chapters of Timothy Budd's description of Leda, which is a Pascal-based language which seeks to support imperative, OO, functional and inference programming styles. I don't think that it highlights any voids in FPC, but some of the examples are thought-provoking (as in "that can't be efficient").

Specifically, when discussing graphs he uses a functional style, where if he wants to count the number of edges attached to a vertex he uses the function to return a subgraph, explicitly counting the elephants before freeing them.

Now FPC could obviously do that with e.g. an overloaded := operator. But
is there any way to handle the special case of two functions, one of
which returns a dynamic array and the other the number of elements,
without having to give them different names or parameters?

http://web.engr.oregonstate.edu/~budd/Books/leda/index.html

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

leledumbo
to Mark Morgan Lloyd
Now FPC could obviously do that with e.g. an overloaded := operator. But is there any way to handle the special case of two functions, one of which returns a dynamic array and the other the number of elements, without having to give them different names or parameters?

With the availability of casts (including OOP style casts), I don't think it's possible to overload simply from return type. Even in the current implementation, there are cases when the compiler can't decide which overloaded function to call. For instance, when the same parameter position of two functions are on the same inheritance line. Moreover, return values don't need to be assigned, it can be safely ignored. In that case, which overloaded function one has to call?

Your case doesn't worth overloaded function, as the returned values has different meaning.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Overloading-functions-tp5720000p5720001.html Sent from the Free Pascal - General mailing list .

Reply by email