www.digitalmars.com         C & C++   DMDScript  

D - type templates

reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
I assume value types have to be internal values not structs etc ? why ? I
need structs
or smalltalk style instanceof (rather than castableto matching)

the code
template L8InterfaceBinding( GUID I : IID_IUnknown ) {
 alias IUnknown if_type;
 alias ComObject L8ComObj;
}
causes errors
lib\win32\l8night\com\helpers.d(11): template L8InterfaceBinding(GUID I :
IID_IU
nknown) integral type expected for value-parameter, not GUID

how does D deal with this if I used
template foo ( I : IUnknown ) {
}
template foo ( I : IDispatch ) {
}

if someone did instance foo( IOleWindow ) myfoo;
would it match with IUnknown rather than reporting an error that there was
not foo for IOleWindow [which is what I want] {all part of my templates to
create automatic QueryInterface}
Aug 02 2003
parent reply "Walter" <walter digitalmars.com> writes:
Value parameters to templates can only be integral types, not
structs. -Walter

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bgh5mc$n3m$1 digitaldaemon.com...
 I assume value types have to be internal values not structs etc ? why ? I
 need structs
 or smalltalk style instanceof (rather than castableto matching)

 the code
 template L8InterfaceBinding( GUID I : IID_IUnknown ) {
  alias IUnknown if_type;
  alias ComObject L8ComObj;
 }
 causes errors
 lib\win32\l8night\com\helpers.d(11): template L8InterfaceBinding(GUID I :
 IID_IU
 nknown) integral type expected for value-parameter, not GUID

 how does D deal with this if I used
 template foo ( I : IUnknown ) {
 }
 template foo ( I : IDispatch ) {
 }

 if someone did instance foo( IOleWindow ) myfoo;
 would it match with IUnknown rather than reporting an error that there was
 not foo for IOleWindow [which is what I want] {all part of my templates to
 create automatic QueryInterface}
Aug 02 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
it would be nice if IID was an integral type would require the compiler to
link some symbols from libs or eval some of the code,
is complex ?

the Generics in Java allow <? extends FOO> and <? super FOO> to bound the
classes
might be nice to have
template one( T: Derived ) { } // matchs derived ONLY
template one( T: extends Derived ) { } // matchs  sub clases (but not
derived)
template one( T: super Derived ) { } // matchs super classes (but not
derived)
template one( T: Derived | super Derived ) { } // matchs derived or super
classes


"Walter" <walter digitalmars.com> wrote in message
news:bgi799$1lkv$1 digitaldaemon.com...
 Value parameters to templates can only be integral types, not
 structs. -Walter

 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bgh5mc$n3m$1 digitaldaemon.com...
 I assume value types have to be internal values not structs etc ? why ?
I
 need structs
 or smalltalk style instanceof (rather than castableto matching)

 the code
 template L8InterfaceBinding( GUID I : IID_IUnknown ) {
  alias IUnknown if_type;
  alias ComObject L8ComObj;
 }
 causes errors
 lib\win32\l8night\com\helpers.d(11): template L8InterfaceBinding(GUID I
:
 IID_IU
 nknown) integral type expected for value-parameter, not GUID

 how does D deal with this if I used
 template foo ( I : IUnknown ) {
 }
 template foo ( I : IDispatch ) {
 }

 if someone did instance foo( IOleWindow ) myfoo;
 would it match with IUnknown rather than reporting an error that there
was
 not foo for IOleWindow [which is what I want] {all part of my templates
to
 create automatic QueryInterface}
Aug 03 2003
next sibling parent "Walter" <walter digitalmars.com> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bgk4i4$ab4$2 digitaldaemon.com...
 it would be nice if IID was an integral type would require the compiler to
 link some symbols from libs or eval some of the code,
 is complex ?

 the Generics in Java allow <? extends FOO> and <? super FOO> to bound the
 classes
 might be nice to have
 template one( T: Derived ) { } // matchs derived ONLY
 template one( T: extends Derived ) { } // matchs  sub clases (but not
 derived)
 template one( T: super Derived ) { } // matchs super classes (but not
 derived)
 template one( T: Derived | super Derived ) { } // matchs derived or super
 classes
Those are all good ideas, and workable in the compiler. But I hesitate to put them in unless there's a proven advantageous use of them.
Aug 04 2003
prev sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
What possible use could there be for a template that could only be
instantiated with one type?

It'd be far more convenient to make

template one( T: Derived ) { } matches Derived or any subclass of Derived,
or anything that provides Derived as an interface.

It'd be great if there were no inherent limitations with generics such as
values can only be simple ints, not structs.  All structs have a nice
compare method accessable at compile time!

Also it'd be great if templates were eliminated from the language and true
generics allowed anywhere a type is allowed.  I was never a fan of the $
syntax but something *like* that would be great.

I find C++ syntax for templates very limiting in actual use, and D's
templates are even worse since there is no implicit instantiation
whatsoever, and otherwise it's functionally identical to C++ templates.
There has to be some other language out there that has gotten this right,
something that can be used as a model.

Sean

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bgk4i4$ab4$2 digitaldaemon.com...
 it would be nice if IID was an integral type would require the compiler to
 link some symbols from libs or eval some of the code,
 is complex ?

 the Generics in Java allow <? extends FOO> and <? super FOO> to bound the
 classes
 might be nice to have
 template one( T: Derived ) { } // matchs derived ONLY
 template one( T: extends Derived ) { } // matchs  sub clases (but not
 derived)
 template one( T: super Derived ) { } // matchs super classes (but not
 derived)
 template one( T: Derived | super Derived ) { } // matchs derived or super
 classes


 "Walter" <walter digitalmars.com> wrote in message
 news:bgi799$1lkv$1 digitaldaemon.com...
 Value parameters to templates can only be integral types, not
 structs. -Walter

 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bgh5mc$n3m$1 digitaldaemon.com...
 I assume value types have to be internal values not structs etc ? why
?
 I
 need structs
 or smalltalk style instanceof (rather than castableto matching)

 the code
 template L8InterfaceBinding( GUID I : IID_IUnknown ) {
  alias IUnknown if_type;
  alias ComObject L8ComObj;
 }
 causes errors
 lib\win32\l8night\com\helpers.d(11): template L8InterfaceBinding(GUID
I
 :
 IID_IU
 nknown) integral type expected for value-parameter, not GUID

 how does D deal with this if I used
 template foo ( I : IUnknown ) {
 }
 template foo ( I : IDispatch ) {
 }

 if someone did instance foo( IOleWindow ) myfoo;
 would it match with IUnknown rather than reporting an error that there
was
 not foo for IOleWindow [which is what I want] {all part of my
templates
 to
 create automatic QueryInterface}
Aug 05 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Ok, this is my reply to Mike Wynn's post:

Yes, I can see where occasionally you'd want to use the exact type, say, for
specialization.

Right now not very much stuff is constructable in place in such a way that
the compiler could build the structure at compile time if initialized with
all compile time constants.  Hell, static array initialization usually
doesn't work either.

A number of languages have improvements in generics versus, say, C++
templates.

One is Transframe:  http://www.visviva.com/transframe/intro.htm

Really, if I could find a language that had everything I wanted, I'd be
using it.  ;)

Sean



----- Original Message ----- 
From: "Sean L. Palmer" <palmer.sean verizon.net>
Newsgroups: D
Sent: Tuesday, August 05, 2003 6:06 PM
Subject: Re: type templates


 What possible use could there be for a template that could only be
 instantiated with one type?
as one specialisation of a template, or to hold info about that type and no other. I do not want to create a template that only hold one type (that I can use a class for [almost]) but I do want to be selecting the template I use based on the class not the class and possible sub classes.
 It'd be far more convenient to make

 template one( T: Derived ) { } matches Derived or any subclass of Derived,
 or anything that provides Derived as an interface.
that is what we have now, for most uses it is fine, just like java's instance of is realy "castable-to" so Java 1.1 added obj.class == CLASSNAME.class to allow the use to check that obj was actually and instance of CLASSNAME and not a subclass. (smalltalk instanceof) I feel that it would be good to fully specify the matching types for a template I can do ... template aaa( T : Base) { ..... T_base ... } template aaa( T : Derived ) { ..... T_derived ... } but T_derived can not be A special case for just derived and not things derived from derived. without template aaa( T : DerivedDerived ) { ..... cut'n'past or alias to T_base ... } but I'd have to do that for all possible derivitives of derived and might miss one this is not very relivant if you've using templates in your code, BUT it comes into force when templates use templates template bbb( T ) { alias instance aaa(T) constrainst; .... etc ... }
 It'd be great if there were no inherent limitations with generics such as
 values can only be simple ints, not structs.  All structs have a nice
 compare method accessable at compile time!
I agree, at first I thought that the issue was compile time eval of the struct values but that's obviously total rubbish, there would have to be a limitation on the value and it would have to be a struct of structs or values fully resolvable at compile time, a struct with pointers or obj refs would not be useble within a template.
 Also it'd be great if templates were eliminated from the language and true
 generics allowed anywhere a type is allowed.  I was never a fan of the $
 syntax but something *like* that would be great.

 I find C++ syntax for templates very limiting in actual use, and D's
 templates are even worse since there is no implicit instantiation
 whatsoever, and otherwise it's functionally identical to C++ templates.
 There has to be some other language out there that has gotten this right,
 something that can be used as a model.
which language (s) do you feel have it right, I'd be interested to look at them ?
Aug 08 2003