OmniThreadLibrary forum
News: SMF - Just Installed!
 
*
Welcome, Guest. Please login or register. May 17, 2012, 05:59:52 PM


Login with username, password and session length


Pages: [1]   Go Down

Author Topic: Error in OtlSync XE2  (Read 956 times)

Jens01

  • Newbie
  • *
  • Posts: 5
    • View Profile
Error in OtlSync XE2
« on: December 02, 2011, 09:06:47 AM »

The last update gives me a error E2089 in line 663 and 741 and E2250 in 742.
Code: [Select]
Result := T(GetTypeData(PTypeInfo(TypeInfo(T)))^.ClassType.Create);
Jens
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #1 on: December 02, 2011, 11:22:50 AM »

Will fix. It is incredibly hard to keep Generics stuff compatible from D2009 to XE2 :(
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #2 on: December 02, 2011, 11:28:29 AM »

Weird, it compiles fine in my XE2. Which update level do you have installed?
Logged

cjsalamon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Error in OtlSync XE2
« Reply #3 on: December 02, 2011, 04:27:10 PM »

I'm having the same problem and I tracked it down. I'm guessing that Delphi added constraints to generics at some point. When they did that they enforced them, so that means that you can't call Create on a type that doesn't have the correct constraints. So you'll need to specify that T in Atomic and Locked is a class type that has a constructor like:

  Atomic<T: class, constructor> = class
  Locked<T: class, constructor> = record

That alone will make this compile. However a bonus of the addition of constraints is that you don't have to go through all that crazy type info stuff to create an object of type T. Now you can just call T.Create.

I had to make these changes myself, so I've attached a diff with the changes.

Here's my version info if you want to try to duplicate:
Delphi XE2 Update 2
Version 16.0.4316.44803
on Windows Vista.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #4 on: December 03, 2011, 03:04:36 AM »

I specifically don't want those constraints to be applied because Atomic also has to work with interfaces and Locked has to work with interfaces, records and everything else.

Please tell me which XE2 are you running - original, Update 1 or Update 2.
Logged

Jens01

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error in OtlSync XE2
« Reply #5 on: December 03, 2011, 06:45:54 AM »

Sorry for my late answer. The reply-mail was in my spam-folder, don't know why.

Quote
Which update level do you have installed?
I tried to compile the revision 1057 and today 1058 with the known errors.
1058 :
Code: [Select]
Result := T(GetTypeData(PTypeInfo(TypeInfo(T)))^.ClassType.Create); -> E2089

My Delphi version is XE2 with update 2 on win7-64  (Compaq).

Jens
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #6 on: December 03, 2011, 07:46:32 AM »

It looks like they changed it between Update 1 and Update 2. I'm still running Update 1 and it works here.

I'll find some way to fix this.
Logged

Jens01

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error in OtlSync XE2
« Reply #7 on: December 03, 2011, 07:55:37 AM »

Quote
It looks like they changed it between Update 1 and Update 2.
Yes I'm sure they do! After I installed the 2. update I had to fix some code in my generic-lists!
Logged

Jens01

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error in OtlSync XE2
« Reply #8 on: December 03, 2011, 10:43:06 AM »

cjsalamon gives the solution. First I overlook his patch.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #9 on: December 03, 2011, 10:45:00 AM »

Let me state again: This patch makes Atomic and Locked not working with interfaces (i.e. one cannot use Atomic<IInterface> or Locked<IInterface>) which is exactly why I dropped generic constraints in the first place.
Logged

Jens01

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error in OtlSync XE2
« Reply #10 on: December 03, 2011, 10:56:20 AM »

Aah, sorry.  :-[
Logged

cjsalamon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Error in OtlSync XE2
« Reply #11 on: December 13, 2011, 11:31:58 AM »

I listed my Delphi version info as Update 2 in my original post. It would appear that they further enforced generic type constraints when casting. You can no longer cast a TObject descendant as a generic type T if T isn't constrained as a class type. However, I did find a work around using the RTTI.TValue construct, which has methods for doing generic casts based on TypeInfo data, as well as the ability to get and set the internal value as a generic type. Here's the code:

class function Atomic<T>.Initialize(var storage: T): T;
var
  xInValue, xOutValue: TValue;
begin
  if not assigned(PPointer(@storage)^) then begin
    if PTypeInfo(TypeInfo(T))^.Kind  <> tkClass then
      raise Exception.Create('Atomic<T>.Initialize: Unsupported type');
    Result := Atomic<T>.Initialize(storage,
      function: T
      begin
        xInValue := GetTypeData(PTypeInfo(TypeInfo(T)))^.ClassType.Create;
        xInValue.TryCast(TypeInfo(T), xOutValue);
        Result := xOutValue.AsType<T>;
      end);
  end;
end; { Atomic<T>.Initialize }

I've attached another patch. I haven't been able to test this at run-time, but it does compile. I wonder if that will work?
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Error in OtlSync XE2
« Reply #12 on: December 13, 2011, 02:51:36 PM »

Wow! Incredible!

Yes, this actually works! Thanks!

If you are contributing to StackOverflow, you should go and post this as an answer to my question: http://stackoverflow.com/questions/8355456/creating-object-instance-based-on-unconstrained-generic-type . I will definitely accept it.
Logged

cjsalamon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Error in OtlSync XE2
« Reply #13 on: December 15, 2011, 02:53:11 PM »

No problem, I'm glad it worked. It looks like someone but me to posting the solution on Stack Overflow.
Logged
Pages: [1]   Go Up
 
 

Powered by MySQL Powered by PHP Powered by SMF 2.0.2 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM