Sign in Join

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

Mailing lists / fpc-pascal / 2017-10

Overloaded functions

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

Hi,

I have a problem with overloaded functions. I defined these functions:
function val_(name: string): string;
function val_(name: string): boolean;
function val_(name: string): integer;
function val_(name: string): double;
var
  res: boolean;
begin
  res := val_(option_variable);
end;

I get the following error: Error: Incompatible types: got "ShortString" expected "Boolean". So for some reason the compiler is looking at the first function. The mode is objfpc and I turned ansistrings off. I tested by adding the overload modifier but without result. What else could I try or do? Or is overloading only allowed for different parameter lists?

Rgds, Darius

Reply by email

Vojtěch Čihák

Overloading means that you have functions with the same name but different parameter (or param. list). You can have different return type for each parameter but you can never have the same parameters. V.
______________________________________________________________

Show quoted text 5 lines
Od: Darius Blaszyk Komu: FPC-Pascal users discussions Datum: 28.10.2017 22:58 Předmět: [fpc-pascal] Overloaded functions

Hi,

I have a problem with overloaded functions. I defined these functions:
function val_(name: string): string;
function val_(name: string): boolean;
function val_(name: string): integer;
function val_(name: string): double;
var
  res: boolean;
begin
  res := val_(option_variable);
end;

I get the following error: Error: Incompatible types: got "ShortString" expected "Boolean". So for some reason the compiler is looking at the first function. The mode is objfpc and I turned ansistrings off. I tested by adding the overload modifier but without result. What else could I try or do? Or is overloading only allowed for different parameter lists?

Rgds, Darius

----------

Reply by email

Darius Blaszyk
to Vojtěch Čihák

Thanks! This is in line with what I was suspecting. However, why in the world does the compiler not warn in this case. This should be at least happening I would suppose?

Darius

Vojtěch Čihák schreef op 28 okt '17:

Show quoted text 35 lines
Overloading means that you have functions with the same name but different parameter (or param. list). You can have different return type for each parameter but you can never have the same parameters. V. ______________________________________________________________ > Od: Darius Blaszyk > Komu: FPC-Pascal users discussions > Datum: 28.10.2017 22:58 > Předmět: [fpc-pascal] Overloaded functions > Hi, I have a problem with overloaded functions. I defined these functions: function val_(name: string): string; function val_(name: string): boolean; function val_(name: string): integer; function val_(name: string): double; var res: boolean; begin res := val_(option_variable); end; I get the following error: Error: Incompatible types: got "ShortString" expected "Boolean". So for some reason the compiler is looking at the first function. The mode is objfpc and I turned ansistrings off. I tested by adding the overload modifier but without result. What else could I try or do? Or is overloading only allowed for different parameter lists? Rgds, Darius ---------- _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Unknown author
to Darius Blaszyk
Darius Blaszyk wrote 45 lines
Thanks! This is in line with what I was suspecting. However, why in the world does the compiler not warn in this case. This should be at least happening I would suppose? Darius Vojtěch Čihák schreef op 28 okt '17: > Overloading means that you have functions with the same name but > different parameter (or param. list). You can have different return > type for each parameter but you can never have the same parameters. > > V. > > ______________________________________________________________ > > Od: Darius Blaszyk > > Komu: FPC-Pascal users discussions > > Datum: 28.10.2017 22:58 > > Předmět: [fpc-pascal] Overloaded functions > > > > Hi, > > I have a problem with overloaded functions. I defined these functions: > function val_(name: string): string; > function val_(name: string): boolean; > function val_(name: string): integer; > function val_(name: string): double; > > var >   res: boolean; > begin >   res := val_(option_variable); > end; > > I get the following error: Error: Incompatible types: got > "ShortString" expected "Boolean". So for some reason the compiler is > looking at the first function. The mode is objfpc and I turned > ansistrings off. I tested by adding the overload modifier but without > result. What else could I try or do? Or is overloading only allowed > for different parameter lists? > > Rgds, Darius > >

Agreed. The following incorrect override compiles without any problems. Since the second definition is inaccessible, it should probably get rejected by the compiler, or at least create a warning.

function val_(name: string): string;
begin
end;
function val_(name: string): boolean;
begin
end;

Reply by email