digitalmars.D - Java-like generics in D
- Frank Benoit (17/17) Aug 12 2007 As addition to the existing templates, i think java like generics would
- Henning Hasemann (7/7) Aug 12 2007 Do you mean something like this?
- Frank Benoit (5/10) Aug 12 2007 no, i meant something like java generics :)
- Radu (7/25) Aug 12 2007 Why on earth should some one want to implement such a hack that Java
- Frank Benoit (14/19) Aug 12 2007 Several times, i tried to use templates in D. And each time, again i
- Robert Fraser (6/18) Aug 12 2007 Is object code duplication a huge problem? Are you running into all sort...
- Bill Baxter (10/32) Aug 12 2007 I think that's the point. If all you're writing is a collection class
- Regan Heath (4/24) Aug 12 2007 I wonder if the proposed D macro feature might help implement this sort
- Reiner Pope (28/49) Aug 13 2007 It certainly makes sense. There's also the point (or is this what you
As addition to the existing templates, i think java like generics would be very helpful for D. The generic could be like this: 1) use the new keyword 'generic' instead of 'template' 2) Typeparameter can only be object or interface types 3) the compiled code does not know the exact type of the instantiation parameter. (type erasure) 4) the compiler 'inserts' missing casts for the given instantiation type parameters Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations. 6) the compiler can generate the object code independantly of the intantiation code 7) no more errors like 'forward reference' for generic, because the generic can compile without the knowledge of the exact types. Does that make sense?
Aug 12 2007
Do you mean something like this? http://www.digitalmars.com/d/phobos/std_boxer.html Henning -- GPG Public Key: http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851 Fingerprint: 344F 4072 F038 BB9E B35D E6AB DDD6 D36D 4191 1851
Aug 12 2007
Henning Hasemann schrieb:Do you mean something like this? http://www.digitalmars.com/d/phobos/std_boxer.html Henningno, i meant something like java generics :) http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html Probably i missed your point. Frank
Aug 12 2007
Why on earth should some one want to implement such a hack that Java generics are? I think you completely miss understand how templates (generic code) work and how bad the Java implementation is. On .Net at least they are more than just compiler syntactic sugar. So no, it does not make any sense! Radu Frank Benoit wrote:As addition to the existing templates, i think java like generics would be very helpful for D. The generic could be like this: 1) use the new keyword 'generic' instead of 'template' 2) Typeparameter can only be object or interface types 3) the compiled code does not know the exact type of the instantiation parameter. (type erasure) 4) the compiler 'inserts' missing casts for the given instantiation type parameters Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations. 6) the compiler can generate the object code independantly of the intantiation code 7) no more errors like 'forward reference' for generic, because the generic can compile without the knowledge of the exact types. Does that make sense?
Aug 12 2007
Radu schrieb:Why on earth should some one want to implement such a hack that Java generics are? I think you completely miss understand how templates (generic code) work and how bad the Java implementation is. On .Net at least they are more than just compiler syntactic sugar. So no, it does not make any sense!Several times, i tried to use templates in D. And each time, again i meet these problems: - errors about forward reverences - increasing executable size by massive template instantiation with differnt types. So my understanding is: * templates are perfect to parametrize with primitives, where a different type parameter generated different binary code. (optimized code) * templates are used to make compile time features possible. But if you make something like a simple container, they are not so nice. A generic would be more appropriate in this case. In this case i wish i had that java hack available. Thats why i though generics as an addition to templates could be a good thing.
Aug 12 2007
Frank Benoit wrote:Radu schrieb:First one is a bug, so no need to invent a new way to write generic code but rather fix the bug. I have to see a case where size bloat really does matter.Why on earth should some one want to implement such a hack that Java generics are? I think you completely miss understand how templates (generic code) work and how bad the Java implementation is. On .Net at least they are more than just compiler syntactic sugar. So no, it does not make any sense!Several times, i tried to use templates in D. And each time, again i meet these problems: - errors about forward reverences - increasing executable size by massive template instantiation with differnt types.So my understanding is: * templates are perfect to parametrize with primitives, where a different type parameter generated different binary code. (optimized code) * templates are used to make compile time features possible.templates are perfect for any parametrized type as we need to write generic code that performs well and also write smart generic code as D templates are Turing complete.But if you make something like a simple container, they are not so nice. A generic would be more appropriate in this case. In this case i wish i had that java hack available. Thats why i though generics as an addition to templates could be a good thing.You can certainly emulate generics by specializing your templates to void* or Object and I think even the casting can be done using D templates and meta programming functions. Java generics are implemented as a hack in the compiler and spec as the JVM doesn't really know anything about the type your container is using, everything is just Object with some casts, this comes from lack of support on the bytecode level and the need to keep backwards compatibility. This is why generics have so little usage across the language as you're limited by lack of expressiveness and power offered otherwise by true generic code.
Aug 12 2007
Reply to Frank,But if you make something like a simple container, they are not so nice. A generic would be more appropriate in this case. In this case i wish i had that java hack available. Thats why i though generics as an addition to templates could be a good thing.I think I have wanted something along this line: I would like to be able to have template that end up as identical be just aliases to each other. for instance, with the template: |template Type(T) |{ | T Read(Stream s) | { | T ret; | s.ReadExact(&ret, T.sizeof); | return ret; | } |} Type!(dchar).Read and Type!(int).Read are exactly the same code (read and return 32bits) the same can be said for templates that work on classes; when two types are used that derive from the same base and the base has all of the methods that the template uses, then the codes is exactly the same. It would be nice to have some way to tell a template what parts of a type to specialize on (size, how to call a given set of methods, etc.) and then only when these are different will new code be generated) This would work vary well with something like a generic container for objects; while instances of the container take and return a given type (as the programer sees it), in fact the code is the same for all cases and it only deals with general objects.
Aug 12 2007
BCS wrote:Reply to Frank,.Net does something like this as it shares specializes code for a given class across the binary JIT image, this could be done in D at the linker level as an optimization pass. Could be an interesting feature.But if you make something like a simple container, they are not so nice. A generic would be more appropriate in this case. In this case i wish i had that java hack available. Thats why i though generics as an addition to templates could be a good thing.I think I have wanted something along this line: I would like to be able to have template that end up as identical be just aliases to each other. for instance, with the template: |template Type(T) |{ | T Read(Stream s) | { | T ret; | s.ReadExact(&ret, T.sizeof); | return ret; | } |} Type!(dchar).Read and Type!(int).Read are exactly the same code (read and return 32bits) the same can be said for templates that work on classes; when two types are used that derive from the same base and the base has all of the methods that the template uses, then the codes is exactly the same.It would be nice to have some way to tell a template what parts of a type to specialize on (size, how to call a given set of methods, etc.) and then only when these are different will new code be generated) This would work vary well with something like a generic container for objects; while instances of the container take and return a given type (as the programer sees it), in fact the code is the same for all cases and it only deals with general objects.
Aug 12 2007
Frank Benoit Wrote:As addition to the existing templates, i think java like generics would be very helpful for D.I think that's an interesting, but wholly unnecessary idea. Generics were added to Java because first-class templates were an impossibility given the bytecode structure. They're a workaround, not a feature to be emulated in other languages.Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations.Is object code duplication a huge problem? Are you running into all sorts of code bloat in your projects? D programs _do_ tend to wax a little on the large side, but code bloat is hardly the primary factor of this.6) the compiler can generate the object code independantly of the intantiation code7) no more errors like 'forward reference' for generic, because the generic can compile without the knowledge of the exact types.there's the one major advantage you've cited, but I don't thin we need a language feature as a workaround for a bug.Does that make sense?I get the feeling they would be generally unused and woefully underpowered compared to templates. Look at the uses for generics in Java: almost exclusively in collections and wrapper classes. I've written maybe two generic classes I can think of, their extensibility to user code is limited. D's templates can do _so_ much more.
Aug 12 2007
Robert Fraser wrote:Frank Benoit Wrote:As addition to the existing templates, i think java like generics would be very helpful for D.I think that's an interesting, but wholly unnecessary idea. Generics were added to Java because first-class templates were an impossibility given the bytecode structure. They're a workaround, not a feature to be emulated in other languages.Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations.Is object code duplication a huge problem? Are you running into all sorts of code bloat in your projects? D programs _do_ tend to wax a little on the large side, but code bloat is hardly the primary factor of this.I get the feeling they would be generally unused and woefully underpowered compared to templates. Look at the uses for generics in Java: almost exclusively in collections and wrapper classes.I think that's the point. If all you're writing is a collection class then maybe you don't really need the full power / bloat of D templates. In C++ there's some sort of trick they use to avoid template bloat in which a specialization for void* is used to implement the template for all pointer types. Then the only thing that actually gets instantiated multiple times is some lightweight interface code that does casting to and from UserType to void*. I think that's essentially what generics do for you under the hood. The same approach should work in D, I would think. --bb
Aug 12 2007
Frank Benoit wrote:As addition to the existing templates, i think java like generics would be very helpful for D. The generic could be like this: 1) use the new keyword 'generic' instead of 'template' 2) Typeparameter can only be object or interface types 3) the compiled code does not know the exact type of the instantiation parameter. (type erasure) 4) the compiler 'inserts' missing casts for the given instantiation type parameters Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations. 6) the compiler can generate the object code independantly of the intantiation code 7) no more errors like 'forward reference' for generic, because the generic can compile without the knowledge of the exact types. Does that make sense?I wonder if the proposed D macro feature might help implement this sort of thing... Regan
Aug 12 2007
Frank Benoit wrote:As addition to the existing templates, i think java like generics would be very helpful for D. The generic could be like this: 1) use the new keyword 'generic' instead of 'template' 2) Typeparameter can only be object or interface types 3) the compiled code does not know the exact type of the instantiation parameter. (type erasure) 4) the compiler 'inserts' missing casts for the given instantiation type parameters Advantages: 5) "lightweight templates", because of no more object code duplication for new instantiations. 6) the compiler can generate the object code independantly of the intantiation code 7) no more errors like 'forward reference' for generic, because the generic can compile without the knowledge of the exact types. Does that make sense?It certainly makes sense. There's also the point (or is this what you generic type. But this is a job for current D: in fact, I had been hoping for compile-time reflection in order to allow this, and I have started writing an implementation. The basic idea is that there's a wrapper template, with prototype as follows: template GenericOf(alias Impl, Args...) The aim is for the GenericOf template to make one (actually, currently it makes two -- one for the actual implementation, and one with distinctive types so that the template parameters can be recognised) instantiation of Impl, and expose a template which wraps this instantiation, providing conversions to and from the base types, so that no conversions have to be done at the call site. I don't know exactly how the C++ version of this -- using a void* instantiation -- works, but I plan to make something more expressive. The Args... tuple specifies for each argument what requirements are placed on the types (this currently just means the base type, and wrapping for structurally-conforming types if desired), mimicking template parameter specialisations, and allowing functions specific on that type to be called (not possible if you just use a void* instantiation). In addition, it is neat because you still write your collection as a template, and just expose the generic with a line like alias GenericOf!(MyCollection, ParameterRequirements) MyGenericCollection; -- Reiner
Aug 13 2007