Sign in Join

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

Mailing lists / fpc-pascal / 2003-10

fpc 1.0 and 1.1 incompatibility

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Vincent Snijders

Hi,

The program below can be compiled with fpc 1.0.10, but not with this week's fpc 1.1.

With fpc 1.0.10 it gives the following output, (which is as I would expect).
TSubClass.Create
TBase.Create

fpc 1.1 gives the following error message:
overloadedcontructor.pas(36,32) Error: Can't determine which overloaded function to call overloadedcontructor.pas(19,19) Hint: Found declaration: constructor TBase.Create(TComponent)
overloadedcontructor.pas(26,23) Hint: Found declaration: constructor TSubClass.Create(TStrings) overloadedcontructor.pas(39) Fatal: There were 1 errors compiling module, stopping

How would you classify this incompatibility? A bug in fpc 1.0.10, a bug in fpc 1.1 or something else?

This situation surfaced as I tried to compile lazarus with fpc 1.1. I found out that I compile this source if I changed the call to the constructor to
test := TSubClass.Create(TStrings(nil));

Regards,
Vincent Snijders.

program overloadedcontructor;
{$mode objfpc}{$H+}
uses
  Classes;
type
  TBase = class
    Constructor Create(Owner: TComponent);
  end;
  TSubClass = class(TBase)
    Constructor Create(strings : TStrings);
  end;

{ TBase }

constructor TBase.Create(Owner: TComponent);
begin
  writeln('TBase.Create');
end;

{ TSubClass }

constructor TSubClass.Create(strings: TStrings);
begin
  writeln('TSubClass.Create');
  inherited Create(nil);
end;
var
  test: TBase;
begin
  test := TSubClass.Create(nil);
end.

Reply by email

Matt Emson
to Vincent Snijders
Show quoted text 7 lines
fpc 1.1 gives the following error message: overloadedcontructor.pas(36,32) Error: Can't determine which overloaded function to call overloadedcontructor.pas(19,19) Hint: Found declaration: constructor TBase.Create(TComponent) overloadedcontructor.pas(26,23) Hint: Found declaration: constructor TSubClass.Create(TStrings) overloadedcontructor.pas(39) Fatal: There were 1 errors compiling module, stopping

In a way that makes a lot of sense... however it's a symptom of the way FPC does overloading in objfpc mode. I've no idea why it's broken between 1.0 and 1.1, I can only think it's related to a missing patch?

Explanation: To the compiler, your declaration of the constructor is 'Create( Areference to object )' in both cases. The Object you pass is typed, so the compiler will pick the overloaded constructor that fits the type you are passing. It looks as if the logic concerning the presedence that a constructor has over other overloaded constructors has changed. One would assume that it used to be 'child class overrides the parent class', hence 1.0 works. It seems that 1.1 takes all methods as equal, and passing 'nil' causes a paradox because nil is a possible value for both overloaded constructors.

In Delphi syntax, we have to explicitly define overloaded methods,

procedure x; overload;
procedure x(s: string); overload;

and this situation becomes clearer. I also believe the overloading rules are tighter in Delphi and you wouldn't be able to do what you are doing anyway.

I suggest that your code needs to mark your constuctors as virtual, and that your second class could override the first's constructor and call inherited to pass the correct param. I don't see an advantage to passing completely different classes as params to overloaded constructors otherwise.

Also remember that you can have constructors with different names (Create, CreateStringList, etc) and so you could get around the issue that way.

Hope that elps,

Matt

Reply by email

Peter Vreman
to Matt Emson
Show quoted text 13 lines
> fpc 1.1 gives the following error message: > overloadedcontructor.pas(36,32) Error: Can't determine which overloaded > function to call overloadedcontructor.pas(19,19) Hint: Found > declaration: constructor TBase.Create(TComponent) > overloadedcontructor.pas(26,23) Hint: Found declaration: constructor > TSubClass.Create(TStrings) overloadedcontructor.pas(39) Fatal: There > were 1 errors compiling module, stopping In a way that makes a lot of sense... however it's a symptom of the way FPC does overloading in objfpc mode. I've no idea why it's broken between 1.0 and 1.1, I can only think it's related to a missing patch?

There was a patch too much. I removed the code inserted to fix tw2725. The overloading search was also done for class constructors, but that is also wrong. Now overloading search in parent classes is only triggered by the overload directive, just like delphi.

Reply by email

Vincent Snijders
to Peter Vreman
Show quoted text 19 lines
>> fpc 1.1 gives the following error message: >> overloadedcontructor.pas(36,32) Error: Can't determine which >overloaded> function to call overloadedcontructor.pas(19,19) Hint: >Found> declaration: constructor TBase.Create(TComponent) >> overloadedcontructor.pas(26,23) Hint: Found declaration: >constructor> TSubClass.Create(TStrings) overloadedcontructor.pas(39) >Fatal: There> were 1 errors compiling module, stopping > > In a way that makes a lot of sense... however it's a symptom of the > way FPC > does overloading in objfpc mode. I've no idea why it's broken > between 1.0 and 1.1, I can only think it's related to a missing > patch? > There was a patch too much. I removed the code inserted to fix tw2725. The overloading search was also done for class constructors, but that is also wrong. Now overloading search in parent classes is only triggered by the overload directive, just like delphi.

Peter, did you commit your changes? I just tried and cycled a new compiler etc, but the test program still errors out.

Vincent.

Reply by email

Peter Vreman
to Vincent Snijders
Show quoted text 25 lines
On Thu, 30 Oct 2003 12:55:55 +0100 (CET) "Peter Vreman" wrote: > >> fpc 1.1 gives the following error message: > >> overloadedcontructor.pas(36,32) Error: Can't determine which > >overloaded> function to call overloadedcontructor.pas(19,19) Hint: > >Found> declaration: constructor TBase.Create(TComponent) > >> overloadedcontructor.pas(26,23) Hint: Found declaration: > >constructor> TSubClass.Create(TStrings) overloadedcontructor.pas(39) > >Fatal: There> were 1 errors compiling module, stopping > > > > In a way that makes a lot of sense... however it's a symptom of the > > way FPC > > does overloading in objfpc mode. I've no idea why it's broken > > between 1.0 and 1.1, I can only think it's related to a missing > > patch? > > > > There was a patch too much. I removed the code inserted to fix tw2725. > The overloading search was also done for class constructors, but that > is also wrong. Now overloading search in parent classes is only > triggered by the overload directive, just like delphi. Peter, did you commit your changes? I just tried and cycled a new compiler etc, but the test program still errors out.

The change is now in cvs. Forgot to mention it, but i can't commit from my pc at my job.

Reply by email