Results 1 to 10 of 21

Thread: Difference between FPC Generics and Delphi Generics?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Difference between FPC Generics and Delphi Generics?

    Hi,

    I would like to use generics, yet keep my code compatible with both Delphi and FPC?

    Would that be possible? If so, how to d it?

    Thanks
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2
    I don't think the implementations differ significantly. At least, I am unable to find any documentation of discrepancies. Generics are pretty straightforward to implement in Object Pascal.

    If anything, I would say that the FPC generics are, at worst, a subset of Delphi generics, so anything you do with the FPC ones should port to Delphi without further modification.

  3. #3
    I think you're wrong. I've been looking into it a bit more and I noticed that FPC uses "generic" and "specialize" keywords, while delphi does not. It makes me think that these two flavours are syntactically quite different.

    Has anyone worked with both of them?
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    FPC generics are much stricter than Delphi's implementation. I wouldn't bother with making them compatible. To make them compatible you would need to specialize every generic class

    Code:
    type
    {$ifdef FPC}
     generic TGenericType<T> = class
    {$else}
     TGenericType<T> = class
    {$endif}
      function Test(AArg: T): T;
     end;
     
     TSomeGeneric = {$ifdef FPC}specialize {$endif}TGenericType<longint>;
    
    implementation
    
    function TGenericType.Test(AArg: T): T;
    begin
       result := T*2;
    end;
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5
    Ahh, yeah, looks like the syntax is slightly different because of the reserved words. However, the actual functionality does appear to be a subset.

    I haven't used Delphi generics (since I don't have a later copy of Delphi with them), but from what I have discovered in terms of differences so far:

    1) Generics in FPC are defined using the "generic" keyword preceding the name of the generic in the declaration, and are "specialized" using the "specialize" keyword. In Delphi, the keywords are not used, but the rest of the syntax appears to be the same (analyzed using several comparative examples in both). This appears to be a decision made by the FPC developers due to a potential conflict with other syntactical elements which use the angle bracket syntax "<>".
    2) Generics in Delphi may be specialized in VAR and TYPE declarations. In FPC, they may only be specialized in TYPE declarations. (caveat: it appears you can specialize in a VAR declaration, but there's no way to instantiate the resulting variable, because the constructor expression doesn't understand the "specialized" keyword, or the use of the angle bracket notation).
    3) Generics in FPC may not be specialized in local scope; in Delphi, Generics can be specialized in local scope (ie, in a procedure/function/method's local type declaration).

    That's what I have found so far; there could be some more caveats to those items, as well as more items; it's not meant to be an exhaustive list, just something to get you started, since the documentation is damn sparse on the subject.

  6. #6
    Wow.. thanks. Both of you.

    I wouldn't bother with making them compatible.
    Hmmm.. yes, I was thinking about that. It would be great to have delphi-style generics in FPC (even if it were only in DELPHI mode). It would definitely be a cool feature. But I now understand that it would make no sense to change the FPC implementation to match delphi's. Besides, FPC was WAY earlier with generics support.

    Nice trick to use conditional defines. I will probably use that. I'll probably do it this way:

    Code:
    type
    {$ifdef FPC}generic{$endif} TGenericType<T> = class
      function Test(AArg: T): T;
     end;
     
     TSomeGeneric = {$ifdef FPC}specialize{$endif} TGenericType<longint>;
    
    implementation
    
    function TGenericType.Test(AArg: T): T;
    begin
       result := T*2;
    end;
    Just saves a couple of lines.

    Thanks Murmandamus, for the whole list of differences. Delphi's generics seem a bit more powerfull to me, but I'm not sure how often I will use the extra functionality you mentioned. I think FPC's implementation will do just fine.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  7. #7
    Generics are overrated anyway

    You might save a slight bit of time, but I think it'll only end in tears
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •