It isn't clear to me what the current status about Generics is: http://www.freepascal.org/wiki/index.php/Generics
Are they Delphi conform (.net2)? What's planned? Can somebody explain?
Mailing lists / fpc-devel / 2006-04
Generics
36 messages · 12 people · –
- FFlávio Etrusco 10
- MMarco van de Voort 6
- FFlorian Klaempfl 4
- DDanny Milosavljevic 3
- MMicha Nelissen 3
- UUnknown author 3
- +6 more
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
It isn't clear to me what the current status about Generics is: http://www.freepascal.org/wiki/index.php/Generics
2.1.1 contains pre alpha code, they aren't usable yet.. It is subject to be finished with 2.2.
Are they Delphi conform
Is there already a specification how they will look like in Delphi?
(.net2)? What's planned? Can somebody explain?
They are more like C++ templates.
Florian Klaempfl wroteIs there already a specification how they will look like in Delphi?
I think no. But there is already a specification for C#. I guess the Delphi syntax won't be differ to much from the C# definition: http://qc.borland.com/wc/qcmain.aspx?d=11168
2.1.1 contains pre alpha code, they aren't usable yet.. It is subject to be finished with…
Florian Klaempfl wrote 8 lines
What? Do you mean that FPC implementation of generics wouldn't be based on "type erasure" but on beefed-up-precompiler-macros? Please say I got it all wrong :-o
-Flávio
What? Do you mean that FPC implementation of generics wouldn't be based on "type erasure" but on beefed-up-precompiler-macros? Please say I got it all wrong :-o
Do you have documents about "type erasure" in native languages where e.g. a string is not an object ?
What? Do you mean that FPC implementation of generics wouldn't be based on "type erasure"…
Flávio Etrusco wrote 24 lines
Generics are "type safe".
What? Do you mean that FPC implementation of generics wouldn't be based on "type erasure"…
Flávio Etrusco wrote 12 lines
You want crippled generics regarding speed and usage like C# has? You can do this with "real" templates as well using an implementation class and wrapper templates.
Do you have documents about "type erasure" in native languages where e.g. a string is not…
Marco van de Voort wrote 6 lines
You have a point. But I don't think supporting generics for native types is a must-have. The syntatic sugar for having typesafe containers and algorithms for _objects_ is all I wanted ;-)
Cheers,
Flávio
Flávio Etrusco wrote 10 lines
Well, that's you :) Other people want different things :)
You have a point. But I don't think supporting generics for native types is a must-have.…
Show quoted text 11 lines
First attempt at a string,object map would already fail. No thanks, too limited.
You want crippled generics regarding speed and usage like C# has? You can do this with…
Florian Klaempfl wrote 17 lines
I've never heard any critics/complaints on the performance of generics in Java (I'm not much into .net 2 yet), but you certainly know better than me.
I guess I was a bit too hasty on the whining, but just thinking about C++ compilation speed when using templates give me cries... TBH I never tried using "type erasure" in C++ templates, but I remember C++ templates' inner workings to have enough pitfalls for me to want to avoid templates at all costs...
Cheers,
Flávio
Show quoted text 14 lines
What compiler? If gcc, gcc does nothing fast, and C++ header model than the bit of codegenerating done.
when using templates give me cries... TBH I never tried using "type erasure" in C++ templates, but I remember C++ templates' inner workings to have enough pitfalls for me to want to avoid templates at all costs...
The "Type erasure" in Java5 is also known to have pitfalls. E.g. check the site of http://www.angelikalanger.com or just throw "type erasure" in google.
I've never heard any critics/complaints on the performance of generics in Java (I'm not…
Flávio Etrusco wrote 20 lines
Well, talking about Java's speed never makes sense. Fact is, if you use classes that you've always an indirection because of the pointer and extra memory manager overhead while for simple tasks you can use simple embed the data into the container data when using real templates.
I guess I was a bit too hasty on the whining, but just thinking about C++ compilation speed when using templates give me cries...
This has nothing to do with templates.
Show quoted text 5 lines
What compiler? If gcc, gcc does nothing fast, and C++ header model than the bit of…
Show quoted text 5 lines
nothing fast ..., _the problem is more_ ... C++ header model....
Well, talking about Java's speed never makes sense. Fact is, if you use classes that…
I hope I'm not being too annoying, I'm just trying to understand better your decision/opinion. If you think this discussion is useless please speak and I'll shut up ;-)
Well, talking about Java's speed never makes sense.
LOL, I agree :-)
Fact is, if you use classes that you've always an indirection because of the pointer and extra memory manager overhead while for simple tasks you can use simple embed the data into the container data when using real templates.
I know, and using "type erasure" will even frequently force the use of inheritance (virtual/dynamic methods) that will add a little more overhead, but thing is I can't see much use for generics for low level tasks.
There are relatively few native types that is usually ok duplicate the whole code if it need be. And it seems to me that absolutely most of the times we are working with classes instead of just single primitive variables, so I still fail to see where "generic for non-objects" would be a big loss.
> I guess I was a bit too hasty on the whining, but just thinking about > C++ compilation speed when using templates give me cries... This has nothing to do with templates.
I've read the opposite many times, but I know that it's much related to C++ pre-processor, headers and linking models, and -again- I'm no C++ or compilers expert. If you say it won't be an issue in FPC, I'm happier then :-)
However, I'm still worried about the bloat that templates tend to add to the binary/executable and about the difficulty of debugging templates (not to say that I prefer name/symbol resolution only through inheritance instead of pre-compiler/macro so much better)...
Cheers,
Flávio
nothing fast ..., _the problem is more_ ... C++ header model....
Marco van de Voort wrote 7 lines
VisualC++ 6.
I know it's much related to C header model and linking, but I never saw any "complete research" about how much these affect the problem, and I'm afraid some of the problems can't just be avoided....
The "Type erasure" in Java5 is also known to have pitfalls. E.g. check the site of http://www.angelikalanger.com or just throw "type erasure" in google.
Thanks, I'll read it right now :-)
Cheers,
Flávio
Show quoted text 9 lines
But easily avoided with Pascal with its thorough precompiled system.
I hope I'm not being too annoying, I'm just trying to understand better your…
Hi,
Flávio Etrusco wrote 16 lines
but thing is I can't see much use for generics for low level tasks.
You need them for any kind of compile-time-type-checked collections... so as long as you only do I/O port programming, you are safe... no wait, only until you try to store the conversation in a list :)
There are relatively few native types that is usually ok duplicate the whole code if it need be.
No, the computer must do all the work it can to enable me to think of what I actually want to do. If by "Duplicate", you mean _automatic_ duplication by the compiler, then I agree...
And it seems to me that absolutely most of the times we are working with classes instead of just single primitive variables,
are strings classes or primitive in your mind?
so I still fail to see where "generic for non-objects" would be a big loss.
It is.
Show quoted text 13 lines
It would add the same bloat if I implemented all the required classes by hand... In the ideal case of generics handling.
and about the difficulty of debugging templates (not to say that I prefer name/symbol resolution only through inheritance instead of pre-compiler/macro so much better)...
Depends on if it's done right :)
Cheers, Flávio
cheers,
Danny
Danny Milosavljevic wroteHi,
(...)
Show quoted text 6 lines
You got me confused here.
Show quoted text 6 lines
This is what templates are about, so this is not what I meant.
Show quoted text 5 lines
My mind is FPC, so it's a primitive type.
Show quoted text 5 lines
Exactly. But I guess you mean "the ideal case of templates handling". Using "type erasure" there's no addition of code, just compile-time checks.
Show quoted text 5 lines
Arguably. But considering C++ has almost 15 years of head start and they couldn't get it right yet, I guess it's not an easy task...
I would summarize the pros vs cons like this, please correct me if I'm wrong:
Templates' pros:
- support of primitive types;
- flexibility (ability to call non-virtual and non-related methods, operators, etc);
- ability of coding for speed.
Type erasure generics' pros:
- No additional generated code, no code bloat;
- Code is easier to understand (no additional trips for symbol resolution);
- Easy (possible ;-) to debug.
- Much easier to implement (in the compiler) ;-)
Cheers,
Flávio
PS. BTW I've just realized that not possible to fully replace 'type erasure generics' in the compiler with 'templates'... at least not as efficiently, right?
Show quoted text 11 lines
What I don't understand is all these differences between templates and generics while the only difference is that for classes you do type erasure.
Note that for reference types you can also do type erasure under FPC _AND_ classes are always reference types.
(...) You got me confused here. This is what templates are about, so this is not what I…
Type erasure generics' pros:
What does type erasure mean without a runtime engine (VM) ?
Micha
Micha Nelissen wrote 7 lines
That the "template" is not aware of the instantiated type of the template parameters, they are only accessed/resolved through oop (inheritance/polymorphism).
A VM isn't necessary at all; for instance, J2SE 5 didn't introduce any modification in the VM (for supporting generics or anything), and most of the compiler changes were only needed because of the Java object model.
-Flávio
Show quoted text 5 lines
That's bad. Why not use interfaces instead, then ?
A VM isn't necessary at all; for instance, J2SE 5 didn't introduce any modification in the VM (for supporting generics or anything), and most of the compiler changes were only needed because of the Java object model.
Yes, that's why generics in Java suck ;-). At least, according to (first google hit for "type erasure"):
http://cakoose.com/wiki/type_erasure_is_not_evil
Micha
What I don't understand is all these differences between templates and generics while the…
Marco van de Voort wrote 12 lines
First, I should have made it more clear that I meant:
- Templates = C++ templates = macros;
- Type erasure generics = Java generics = the template has an equivalent _single_ class, for which templates parameters are resolved by normal OO, and each "template instance" (e.g. TList<TItemType: TComponent>) don't generate any additional code, the instance types are only know at compile-time and checked and exposed for external code.
To summaryze yeat more:
Generics are much simpler to use and to implement, I'm afraid poeple will overuse (and use badly) template flexibitlity, and there's no way to implement 'properties' without getter methods when using "type erasure" through templates.
Show quoted text 5 lines
Can you please be more specific? English is not my native language and I'm having some trouble to understand what you meant.
Best regards,
Flávio
(...) You got me confused here. This is what templates are about, so this is not what I…
Hi,
Flávio Etrusco wrote 12 lines
bla: TList(Integer);
repeat
i := inp(49330);
bla.Add(i);
until i = 0;I meant that no matter what you do, once you need any more than a static number of values remembered, you already need them... (strings are a special case though)
How low is "low level"?
> > There are relatively few native types that is usually ok duplicate the > > whole code if it need be.
Single
Double
Integer
Shortint
Longint
Cardinal
Byte
Char
WideChar
ansistring
widestring
set (all possible set definitions)
enum (all possible enum definitions)
record (all possible record definitions)
array (all possible array definitions)
...
and one is supposed to repeat (at least) all collection object classes for each of them? You can't be serious...
Show quoted text 14 lines
I see. TDictionary(string, TObject) doesn't work then? (one of the most used mappings with cgis, at least)
Show quoted text 9 lines
Those TObject-derived type parameters which don't have any of it's methods called by the generic (on the type parameter) can be handled that way, yes. Nothing else.
Show quoted text 9 lines
They are working pretty well now (in gcc)...
Show quoted text 22 lines
A mixture is best: use the same generic specialization where possible and new specializations otherwise...
cheers,
Danny
Danny Milosavljevic wrote 26 lines
Do you honestly have any use for generics supporting primitive types other than writing half a dozen containers?
Show quoted text 25 lines
You can't be serious. Why do you need containers or whatever code so specific and optimized for all these types? And do you wanna count many TObject descendants vs. native types (including records) does the e.g. FCL include?
Show quoted text 6 lines
Why do the String in this case need to be anything other than a String?
Show quoted text 10 lines
I'm talking about debbuging, so besides gcc, gdb and some other user interface must be working pretty well too; what are they? I know that VisualStudio 2003 certainly isn't (I didn't try VS 2005 yet, but I doubt it is any better is this respect...).
Show quoted text 6 lines
If this was the case of course I would have be glad :-) I'd still be afraid to have FCL becoming anything like the STL (i.e. as horrible and impossible to read as it), though...
Following the URL Micha sent in a previous reply, I've just read that .net 2 supports generics for native type by recompiling the templates only in this case. Interesting...
http://cakoose.com/wiki/microsoft_clr
Cheers,
Flávio
First, I should have made it more clear that I meant: - Templates = C++ templates =…
Show quoted text 29 lines
The short answer on everything you say: Forget Java generics.
FPC is a compiled language where not all types are classes. The only way to implement it is like C++ templates. It is a known fact that generics will introduce code bloat, but that is still less than the 15-20mb virtual machines you need for Java/C#
Do you honestly have any use for generics supporting primitive types other than writing…
Hi,
Flávio Etrusco wrote 30 lines
Probably not, because my main use is containers. I cringe when I have TStringList but no TIntegerList, for example... (and all the others)
Show quoted text 29 lines
In order for it not to be slow :)
And do you wanna count many TObject descendants vs. native types (including records) does the e.g. FCL include?
There are many...
but not all of them will have a specialization automatically from the start, only on "use".
Show quoted text 10 lines
I don't understand... string is a primitive type, so it wouldn't work in this example, no?
Show quoted text 16 lines
Good question...
Show quoted text 16 lines
yeah, nice :)
cheers,
Danny
It isn't clear to me what the current status about Generics is:…
It isn't clear to me what the current status about Generics is: http://www.freepascal.org/wiki/index.php/Generics
http://www.dummzeuch.de/delphi/object_pascal_templates/english.html
http://community.borland.com/article/0,1410,27603,00.html
-Bee-
has Bee.ography at
http://beeography.wordpress.com
The short answer on everything you say: Forget Java generics. FPC is a compiled language…
FPC is a compiled language where not all types are classes. The only way to implement it is like C++ templates. It is a known fact that generics will introduce code bloat, but that is still less than the 15-20mb virtual machines you need for Java/C#
Those 'bloat's are not really comparable because they have a different origin.
With templates you can prevent bloat by designing/implementing your data structure with a size parameter, then the generic descendent will only be a bunch of inlined typecasts -> no bloat. Of course, the generic size parameter may waste memory and/or CPU cycles, but that's exactly where the tradeoff has to be made anyway.
Micha
http://www.dummzeuch.de/delphi/object_pascal_templates/english.html…
Bisma Jayadi wrote 6 lines
Ingenious! I really feel stupid for not thinking of something that simple like this before :-)
Cheers,
Flávio
Show quoted text 9 lines
Yup, same respond of mine when first time I read these articles. I never knew (think?) that Delphi can do something like this. Maybe it looks like a bit hack, but I can live with that, and the most important is: it works like the way I wanted. Even C++ compiler does (almost) the same trick. :)
Quote:
Templates are converted by the C++ compiler (actually it is done by the preprocessor, but who cares about this difference?) into copies of the template code. This means that with each usage of a template the whole code for this template will be added to the program again. This could explain why C++ programs are that large. ;-) But this and other similarly complex processes are definitely the reason why C++ compilers are that slow (Typical compile times for a few ten thousand lines of code in Delphi: 5-10 seconds, in C++ several minutes). -----
-Bee-
has Bee.ography at
http://beeography.wordpress.com
Ingenious! I really feel stupid for not thinking of something that simple like this…
Flávio Etrusco wrote 6 lines
Something similar is used in the matrix unit of the rtl (at least for the implementation, see rtl/inc/matrix.pp).
Jonas
Ingenious! I really feel stupid for not thinking of something that simple like this…
Flávio Etrusco wrote 13 lines
Is it possible this way to "derive" 2 (or more) classes in one unit from the same generic ?
Marc
Op Thu, 13 Apr 2006, schreef Marc Weustink:
Show quoted text 5 lines
The matrix unit does it. It uses FPC's preprocessor instead of type aliases, though.
Daniël
Is it possible this way to "derive" 2 (or more) classes in one unit from the same generic…
Marc Weustink wrote 22 lines
From http://community.borland.com/article/0,1410,27603,00.html
The only requirements when implementing templates this way is that each new type should be declared in a separate unit and you should have the sources for the generic classes.
So, no.
Of course if you use macros in fpc, you can. See for example the format shortstring and ansistring implementations.
Vincent.
