Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Anonymous Methods    [ Add a report in this area ]  
Report #:  95215   Status: Closed
TPredicate<T> not compatible to TFunc<T, Boolean>
Project:  Delphi Build #:  15.0.3953.35171
Version:    15.1 Submitted By:   Stefan Glienke
Report Type:  Issue Date Reported:  6/16/2011 4:15:38 AM
Severity:    Extreme corner case Last Updated: 6/17/2011 10:35:57 AM
Platform:    All platforms Internal Tracking #:  
Resolution: Test Case Error (Resolution Comments) Resolved in Build: : 15.0.3953.35171
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
Added by Sysop
<<<<<<<<<<<
Try to compile this code. It causes compile error E2010.
---------
  TMyPredicate1 = reference to function(Arg1: Integer): Boolean;
  TMyPredicate2 = reference to function(Arg1: Integer): Boolean;

procedure TForm1.FormCreate(Sender: TObject);
var
  p1: TMyPredicate1;
  p2: TMyPredicate2;
begin
  p1 := p2; // E2010
end;
---------
>>>>>>>>>>

Try to compile this code:

var
  f: TFunc<Integer, Boolean>;
  p: TPredicate<Integer>;
begin
  p := f;
end;

- expected: it compiles
- actual: E2010 Incompatible types: 'SysUtils.TPredicate<System.Integer>' and 'SysUtils.TFunc<System.Integer,System.Boolean>'
Steps to Reproduce:
definitions are as follows:

TPredicate<T> = reference to function(Arg1: T): Boolean;
TFunc<T1, TResult> = reference to function(Arg1: T): TResult;

That means TPredicate<Integer> has exactly the same signature as TFunc<Integer, Boolean>

I could assign some function with the signature function(i: Integer): Boolean to variables/argument of both types. If I have some method A that accepts TPredicate<T> as argument I cannot assign the result of some method B that returns TFunc<T, Boolean>. So it would require to overload said method B to also have TPredicate<T> as result. This unfortunatly fails because methods cannot be overloaded when just their result type differs.

The other solution would be to hardcast before passing the result to Method A.
Workarounds
Do a hardcast: p := TPredicate<Integer>(f);
Attachment
None
Comments

Tomohiro Takahashi at 6/16/2011 7:22:06 AM -
> - expected: it compiles
Is this a regression?
and, why do you think these types are compatible?

Stefan Glienke at 6/16/2011 9:05:22 AM -
No, it's no regression, this never worked.

Anyway, definitions are as follows:

TPredicate<T> = reference to function(Arg1: T): Boolean;
TFunc<T1, TResult> = reference to function(Arg1: T): TResult;

That means TPredicate<Integer> has exactly the same signature as TFunc<Integer, Boolean>

I could assign some function with the signature function(i: Integer): Boolean to variables/argument of both types. If I have some method A that accepts TPredicate<T> as argument I cannot assign the result of some method B that returns TFunc<T, Boolean>. So it would require to overload said method B to also have TPredicate<T> as result. This unfortunatly fails because methods cannot be overloaded when just their result type differs.

The other solution would be to hardcast before passing the result to Method A.

Tomohiro Takahashi at 6/16/2011 6:41:03 PM -
First of all, for example, this code generate same compile error, because the types are different in Delphi Win32.
---------
  TMyPredicate1 = reference to function(Arg1: Integer): Boolean;
  TMyPredicate2 = reference to function(Arg1: Integer): Boolean;

procedure TForm1.FormCreate(Sender: TObject);
var
  p1: TMyPredicate1;
  p2: TMyPredicate2;
begin
  p1 := p2; // E2010
end;
---------

Stefan Glienke at 6/17/2011 3:35:01 AM -
I didn't even think that simple and was expecting some problem caused by generics. But that's even worse imo.

Stefan Glienke at 6/17/2011 2:35:26 PM -
Actually I disagree on what Jason said in this case.

When you have 2 different definitions of the same procedure signature those 2 actually ARE assignment compatible. The same should apply to method references. Actually if i remember correctly method references were designed to be assignment compatible with normal routines and with method pointers as follows:

type
  TMyFunc1 = function(Arg1: Integer): Boolean;
  TMyFunc2 = function(Arg1: Integer): Boolean;

  TMyEvent1 = function(Arg1: Integer): Boolean of object;
  TMyEvent2 = function(Arg1: Integer): Boolean of object;

procedure TForm1.FormCreate(Sender: TObject);
var
  f1: TMyFunc1;
  f2: TMyFunc2;
  e1: TMyEvent1;
  e2: TMyEvent2;
  p1: TMyPredicate1;
  p2: TMyPredicate2;
begin
  f1 := f2;

  e1 := e2;

  p1 := f1;
  p1 := f2;
  p2 := f1;
  p2 := f2;

  p1 := e1;
  p1 := e2;
  p2 := e1;
  p2 := e2;
end;

This compiles so should when using method references with the same signature. Otherwise thats inconsistency.

Server Response from: ETNACODE01