Overloaded functions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Overloaded functions

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloaded functions

vojtech.cihak

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 <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloaded functions

Darius Blaszyk

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 <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloaded functions

pascalX
On 28/10/17 23:54, Darius Blaszyk wrote:

> 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 <[hidden email]>
>> > Komu: FPC-Pascal users discussions <[hidden email]>
>> > 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;




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal