Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Generics    [ Add a report in this area ]  
Report #:  108181   Status: Closed
Variable typecast and "class" type parameter constraint
Project:  Delphi Build #:  17.0.4625.53395
Version:    17.0 Submitted By:   Blaise Thorn
Report Type:  Basic functionality failure Date Reported:  8/23/2012 10:41:35 AM
Severity:    Commonly encountered problem Last Updated: 4/15/2014 6:46:48 PM
Platform:    All platforms Internal Tracking #:   31408
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
In general, any mutable reference to class instance can be subjected to variable typecast. However, such typecast is groundlessly rejected by the compiler, if the type of the reference happens to be a class-constrained generic type parameter. Which I find especially ridiculous, since an explicit intermediate typecast to TObject solves the problem.

Tags: E2064 E2197
Steps to Reproduce:
// Expected: this program gets compiled; Actual: bogus E2064 and E2197
type G<T: class> = record

procedure Foo(out P: pointer);

procedure Works(out X: TObject);

procedure DoesNot(out X: T);
end;

procedure G<T>.Foo(out P: pointer); begin end;

procedure G<T>.Works(out X: TObject);
begin

NativeUInt(X) := 42;

Foo( pointer(X) )
end;

procedure G<T>.DoesNot(out X: T);
begin

NativeUInt(X) := 42;

// ^- BOGUS E2064 Left side cannot be assigned to

Foo( pointer(X) );

// ^- BOGUS E2197 Constant object cannot be passed as var parameter


// Workaround.

// The transient typecast to TObject should not be required.

NativeUInt(TObject(X)) := 42;

Foo( pointer(TObject(X)) );
end;

end.
Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01