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
    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

  2. #2
    Well, they solve some rather sticky problems at the design paradigm level. For example, ages ago, back before classes, I had a number of different internal linked list methods that I created with Objects (think TP 5.5-5.0). If I wanted singly-linked, doubly-linked, circularly-linked, etc options, I had a different object for each combination of options (well, some of them were actually options in the list). When we got classes, it made it easier to manipulate them syntactically, but I still had a number of classes that I had to support so that I could maintain distinctive functionality. Generics solves the last of the problems I had, so I can actually use just one generic TLinkedList class now, but the fact that it is little more than a glorified high-level macro-like extension still doesn't feel like the optimal solution. I think there is still yet more enhancement and integration to be had in the concept, and it will finally be the sweetest syntactic sugar to solve those sticky kinds of problems.

    It is definitely nice for some things, but you're right; it's not useful or appropriate to use for many things, even though programmers sometimes go overboard trying to do so.

  3. #3
    In Delphi 2009 and above the new Generics.Collections unit is highly useful.

    You can declare containers like this:

    X : TObjectList<TMyClass>;

    It will hold and own the items.

    Then iterate like this:

    Item : TMyClass;
    ...
    for Item in X do
    ...

    I like this style of generic containers as it saves time typing and removes the need of casting. There is also a hashtable called TDictionary<K,V>.
    But I agree that more advanced use of generics can indeed lead to trouble
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  4. #4
    I will probably only use them for list-like datastructures. It's really handy if you don't have to copy the same code for integers, floats, 2d, 3d and 4d vectors.

    Btw, is it possible to use Type Parameter bounds, like java has? It means that you can enforce that your type-parameter T must be a derivative of a specific class, like this:

    Code:
    public class SomeGenericClass <T extends OtherClass> {
        //....
    }
    Last edited by chronozphere; 05-01-2011 at 09:54 AM.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5
    Quote Originally Posted by chronozphere View Post
    Btw, is it possible to use Type Parameter bounds, like java has? It means that you can enforce that your type-parameter T must be a derivative of a specific class, like this:
    A text worth mentioning: http://sjrd.ftp-developpez.com/tutor...i-generics.pdf.

    Answering your question: yes, it is. As you will see in the article, there is few methods to do so. A little extract from the article, which I put below, provides you with an answer:
    Code:
    type
    TStreamGenericType<T: TStream> = class
    end;

  6. #6
    Ah thanks. I'll read that!

    And how about FPC? Does it have this feature too?
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  7. #7
    Looks like Paul Ishenin is updating Delphi mode generics in FPC heavily those days, so it might be safe to just use the Delphi generics style for now
    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
  •