Pascalish-looking generics poll

Now for a bit of hypothetical polling! thinking on the generics/templates syntax, one can’t quite shake the feeling Delphi got the C#/C++ generics/templates syntax. The  < and > can’t be read out loud, and just look like an out of place reuse of the comparison symbols.

Interestingly enough, Pascal had something of a generics syntax (if in a restricted case) with “array of”. I sometimes wonder how reusing that syntax would float, especially combined with some other enhancements f.i. instead :

TQueue<T> = class
var myQueue : TQueue<T>;
myQueue := TQueue<T>.Create;
function Whatever<T>(param : T) : String;

could be more Pascalish (and readable) as

TQueue of T = class
var myQueue : TQueue of T;
myQueue := new TQueue of T;
function Whatever of T(param : T) : String;

And for multiple types with constraints, instead of

TFoo<T: ISerializable; V: IComparable>

wouldn’t something like

TFoo of T (ISerializable), V (IComparable)
// or (Kazantsev Alexey's)
TFoo of (T : ISerializable, IWhatever; V : IComparable)
// or (Schalk's)
TFoo of T, V
where T is ISerializable
where V is IComparable

be more Pascalish?

The clock can’t really be turned back… but what say you?

Pascalish generics syntax

  • I prefer C-style anyway (40%, 69 Votes)
  • I prefer Pascalish style (60%, 103 Votes)
  • I've got a better idea (0%, 0 Votes)

Total Voters: 172

Loading ... Loading ...

24 thoughts on “Pascalish-looking generics poll

  1. The last Example could also be written as:

    TFoo of T, V
    where T is ISerializable
    where V is IComparable

  2. Kazantsev Alexey & Schalk: yes these look better.

    That falls back to something like the function declaration syntax

    TFoo of (T : ISerializable, IWhatever; V : IComparable)

    and this is more readable (out loud)

    TFoo of T, V
    where T is ISerializable, IWhatever
    where V is IComparable

  3. I think the discussion if some language syntax is pascalish or not is kind of philosophical.

    I actually have no problem with translating the lesser/greater symbols into “TFoo of T and V where T is ISerializable and V is Comparable” or something when reading it or telling another person. I like to keep things short and compact (that’s why I totally wish for lambdas in native Delphi) without being too cryptic as you can in C/C++ for example.

  4. @Schalk Maybe
    TFoo of T, V
    with T: ISerializable
    with V: IComparable
    to save keywords. And to give the poor underestimated “with” some new use. 🙂

  5. That’s really C++ style and not C style. No generics in C.

    I’m fine with following the lead set by C++/Java/C#. It doesn’t seem to me to matter much which way you go. But if there is a well used and well known precedent, you may as well follow it.

  6. David Heffernan :

    That’s really C++ style and not C style. No generics in C.

    I’m using C-style liberally to denote C#, C++, Java etc. not just C 😉

    It doesn’t seem to me to matter much which way you go.

    Well at some point is just dilutes the language. Pascal avoided operator soup, C-styles live in it. If you don’t keep to a language to its roots, at some point it just becomes a pointless variation of another language where all you notice are the quirks.

  7. Just like Arrays, the special “file” type has been very much like a generic type since the Turbo Pascal days, and it still uses the “of” syntax.

    type
    TPerson = record
    Name:String;
    Birthday:TDateTime;
    end;

    var
    F:File of TPerson;

  8. Well, I actually thing a “TMyClass of X” syntax has several limitations.

    How would you declare a “TMyClass of X andAlsoOf Y andFurthermore Z” ?

    You can write TFoo<TBar> in the C/C#/Pascal syntaxes. Not that you SHOULD ever do that. I’d like to shoot people who nest generics to absurd levels, because I feel it really makes for “write-only” unmaintainable mess.

    Nevertheless, the syntax should be powerful when you need powerful, and “of” doesn’t work well then.

    Warren

  9. I prefer the C++/C#/Java way 🙂
    the pascal-ish way doesn’t seem any clearer to me, maybe give it a bit more thought? If I wasn’t familiar with generics, both would be quite difficult to understand…

  10. Eric109 :

    ObjectMethodology.com113 :
    Pascalish is winning the pool.

    Stefan Glienke114 :
    btw, the guys over at FPC had the same discussion:

    Looks like Pascalish won the poll there too.

    In the end a syntax similar to the C++/Java one was implemented (note that FPC’s generic support is older than Delphi’s!):

    generic TTest = class

    end;

    and for specializations (which weren’t possible inline at that time):

    type
    TTestLongint = specialize TTest;

    Meanwhile FPC has gained support for Delphi style generics (without keywords and also (partial) inline specialization support), but my plan is to keep FPC’s syntax active in non Delphi modes (and even use “specialize” keyword for inline specializations like methods/procedures). My main motive for this is that Delphi lost the “single pass” concept with this.

    Consider the following code:

    type
    TMyGeneric = class
    end;

    TMyOtherGeneric = class
    end;

    var
    TMyGeneric: Integer; // this is legal in Delphi!
    TMyOtherGeneric: Integer;

    begin
    if TMyGeneric<TMyOtherGeneric{up to here the compiler does not know whether it's a specialization or a "<" operation!} then
    … {ah! it was an "<" operation…}
    if TMyGeneric<TMyOtherGeneric{same case…}.SomeSubType> then
    … {this time it was a specialization … -.- }
    end.

    So the compiler (at least the way it is currently build) needs to backtrack to the point it parsed “TMyGeneric” and restart parsing if the found code does not fit it’s first expectation. This is not yet implemented, but it’s on my ToDo list. For non-Delphi modes the same code will (in the future) read like this:

    begin
    if TMyGeneric<TMyOtherGeneric{this must be an "<" operation!} then

    if specialize {we know now that a specialization must follow} TMyGeneric<specialize TMyOtherGeneric.SomeSubType> then

    end.

    Out of interest: what Syntax would you have preferred for the specialization? You only gave examples for the declaration, but the specialization with “” isn’t very Pascalish either 😉

    And I personally would also have implemented the RTTI attributes differently:

    TSomeClass = class
    property SomeProperty read … write … attributes [Attribute1, Attribute2(arg1, arg2)];
    end;

    Way more Pascalish that these preceding “[…]” blocks that float in the middle of nowhere -.-

    Regards,
    Sven

  11. Ok… now I know another reason why I prefer a more Pascalish syntax: “<” and “>” are used in HTML style markup as well ^^ So now all code again with escapes (and sorry for the noise -.-):

    Declaration syntax:

    generic TTest<T> = class

    end;

    Specialization syntax:

    type
    TTestLongint = specialize TTest<LongInt>

    Example code Delphi:

    type
    TMyGeneric<T> = class
    end;

    TMyOtherGeneric<T> = class
    end;

    var
    TMyGeneric: Integer; // this is legal in Delphi!
    TMyOtherGeneric: Integer;

    begin
    if TMyGeneric<TMyOtherGeneric{up to here the compiler does not know whether it’s a specialization or a “<” operation!} then
    … {ah! it was an “<” operation…}
    if TMyGeneric<TMyOtherGeneric{same case…}<Integer>.SomeSubType&gt then
    … {this time it was a specialization … -.- }
    end.

    Example in non-Delphi modes:

    begin
    if TMyGeneric<TMyOtherGeneric{this must be an "<" operation!} then

    if specialize {we know now that a specialization must follow} TMyGeneric<specialize TMyOtherGeneric<Integer>.SomeSubType> then

    end.

    Regards,
    Sven

  12. @Eric

    Avoiding operator soup is different from choosing which syntax you want to use for a feature. Avoiding operator soup keeps the language simpler and cleaner. Adding generics makes it more complex no matter what syntax you use.

  13. Don’t laugh but VB.NET might have a generics syntax that closer to what Delphi/Pascal could have used, which uses the “of” keyword, like “of Integer”, etc., and since it’s also a keyword in Pascal/Delphi then why not use it here too. However it seems the “of” is only used in the declaration part in VB.NET and other parts still uses C style, which I think could have used “of” too.

    VB.NET generics syntax:
    http://msdn.microsoft.com/en-us/library/w256ka79(v=vs.110).aspx

Comments are closed.