www.digitalmars.com         C & C++   DMDScript  

D - Implicit function template instatiation?

reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Hi there,

I have something like:

--------------------------

void delegate(T) dlgt; this(void delegate(T) _dlgt) { dlgt = _dlgt; } void onCall(T t) { dlgt(t); } } template slot(T) { Slot!(T) slot(void delegate(T) dlgt) { return new Slot!(T)(dlgt); } } <--------------------------- I would like to be able to do something like:
---------------------------

void someroutine(int i) { dosomething(i); } }; mysignal.connect(slot(&receiver.someroutine)); <--------------------------- Anyhow, what I actually have to do, is something ugly, like:
---------------------------

<--------------------------- Any idea how I might so something similar to implicit function template instantiation in C++? Ciao, Nobbi
Apr 25 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Norbert Nemec wrote:

mysignal.connect(slot(&receiver.someroutine));
<---------------------------

Anyhow, what I actually have to do, is something ugly, like:

  

---------------------------
    

<--------------------------- Any idea how I might so something similar to implicit function template instantiation in C++? Ciao, Nobbi

alias slot!(void delegate(int)).slot slot; mysignal.connect(slot(&receiver.someroutine)); ? -- -Anderson: http://badmama.com.au/~anderson/
Apr 25 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
J Anderson wrote:


alias slot!(void delegate(int)).slot slot;

Note that you can overload alias's so you can also go: alias slot!(void delegate(int)).slot aslot; alias slot!(void delegate(double)).slot aslot; ect...
 mysignal.connect(slot(&receiver.someroutine));


 ?

-- -Anderson: http://badmama.com.au/~anderson/
Apr 25 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
J Anderson wrote:

 J Anderson wrote:
 

alias slot!(void delegate(int)).slot slot;

Note that you can overload alias's so you can also go: alias slot!(void delegate(int)).slot aslot; alias slot!(void delegate(double)).slot aslot; ect...

Nice, but not good enough: I would have to overload it for every type separately, which is a bit impractical for a library. What you are showing here is a convenient way for explicit instantiation. What I am looking for is a way for real implicit instantiation. I.e. - like in C++ - a template function is implicitely instantiated with the correct parameters when it is called somewhere.
Apr 25 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Norbert Nemec wrote:

Nice, but not good enough: I would have to overload it for every type
separately, which is a bit impractical for a library.

What you are showing here is a convenient way for explicit instantiation.
What I am looking for is a way for real implicit instantiation. I.e. - like
in C++ - a template function is implicitely instantiated with the correct
parameters when it is called somewhere.
  

If you search the newsgroup you'll find that this doesn't exist in D. It has been brought-up a few times but big W believes it makes the language more complex (and more difficult in the long-run). You can always alias all the primitive types (with the same name) and then the interface of the types you want to make available to the template. Once you've one it once, its a simply matter of a copy/paste/replace to update it for every template you create and things behave like implicit. If you don't like it take it up with big W (for the 15th time but whose counting). -- -Anderson: http://badmama.com.au/~anderson/
Apr 25 2004
next sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c6glb5$25k1$1 digitaldaemon.com...
 Norbert Nemec wrote:

Nice, but not good enough: I would have to overload it for every type
separately, which is a bit impractical for a library.

What you are showing here is a convenient way for explicit instantiation.
What I am looking for is a way for real implicit instantiation. I.e. -


in C++ - a template function is implicitely instantiated with the correct
parameters when it is called somewhere.

If you search the newsgroup you'll find that this doesn't exist in D. It has been brought-up a few times but big W believes it makes the language more complex (and more difficult in the long-run). You can always alias all the primitive types (with the same name) and then the

Is this a good solution? Alias means instance and if you create instances for all primitive types it means a lot of code and probbably most of it isn't used. And what is aprimitive type? int, float, bit... But what about char[], int[], bit[][], bit[char[]] and more complex types? Implicit instantiation would be great (and very ver VERY useful feature) for such a powerful language as D :)
 interface of the types you want to make available to the template.  Once
 you've one it once, its a simply matter of a copy/paste/replace to
 update it for every template you create and things behave like implicit.

 If you don't like it take it up with big W (for the 15th time but whose
 counting).

 --
 -Anderson: http://badmama.com.au/~anderson/

Apr 25 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Ivan Senji wrote:

Is this a good solution? Alias means instance and if you create instances
for all primitive types it means a lot of code and probbably most of
it isn't used. And what is aprimitive type? int, float, bit...
But what about char[], int[], bit[][], bit[char[]] and more complex types?

Implicit instantiation would be great (and very ver VERY useful feature)
for such a powerful language as D :)
  

BTW: You can write something like: template tempT(T) { void temp(inout T value) { } void temp(inout T [] value) //Support for 1D arrays { } } //alias tempT!(bit).temp temp; //Needs a specialized template alias tempT!(byte).temp temp; alias tempT!(ubyte).temp temp; alias tempT!(short).temp temp; alias tempT!(ushort).temp temp; alias tempT!(int).temp temp; alias tempT!(long).temp temp; alias tempT!(ulong).temp temp; //alias tempT!(cent).temp temp; //alias tempT!(ucent).temp temp; alias tempT!(float).temp temp; alias tempT!(double).temp temp; alias tempT!(real).temp temp; alias tempT!(ireal).temp temp; alias tempT!(ifloat).temp temp; //alias tempT!(creal).temp temp; alias tempT!(cfloat).temp temp; alias tempT!(cdouble).temp temp; //alias tempT!(wchar).temp temp; //Confict with object for some reason //alias tempT!(dchar).temp temp; //Confict with object for some reason alias tempT!(Object).temp temp; alias tempT!(char).temp temp; -- -Anderson: http://badmama.com.au/~anderson/
Apr 25 2004
prev sibling parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
J Anderson wrote:

 Norbert Nemec wrote:
 
Nice, but not good enough: I would have to overload it for every type
separately, which is a bit impractical for a library.

What you are showing here is a convenient way for explicit instantiation.
What I am looking for is a way for real implicit instantiation. I.e. -
like in C++ - a template function is implicitely instantiated with the
correct parameters when it is called somewhere.

If you search the newsgroup you'll find that this doesn't exist in D. It has been brought-up a few times but big W believes it makes the language more complex (and more difficult in the long-run). You can always alias all the primitive types (with the same name) and then the interface of the types you want to make available to the template. Once you've one it once, its a simply matter of a copy/paste/replace to update it for every template you create and things behave like implicit.

Well, sorry to hear that. Implicit instantiation is one of the cornerstones of compile-time polymorphism in C++. Sure, it complicates the language and demands quite some intelligence from the compiler, but I believe, it is rather essential. Well - I won't take up fighting for it, right now. If it's really important, it probably will keep itching not me but many others as well, until maybe, someday we can get someone to scratch...
Apr 25 2004
parent reply "Matthew" <matthew.hat stlsoft.dot.org> writes:
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message
news:c6h75b$16r$1 digitaldaemon.com...
 J Anderson wrote:

 Norbert Nemec wrote:

Nice, but not good enough: I would have to overload it for every type
separately, which is a bit impractical for a library.

What you are showing here is a convenient way for explicit instantiation.
What I am looking for is a way for real implicit instantiation. I.e. -
like in C++ - a template function is implicitely instantiated with the
correct parameters when it is called somewhere.

If you search the newsgroup you'll find that this doesn't exist in D. It has been brought-up a few times but big W believes it makes the language more complex (and more difficult in the long-run). You can always alias all the primitive types (with the same name) and then the interface of the types you want to make available to the template. Once you've one it once, its a simply matter of a copy/paste/replace to update it for every template you create and things behave like implicit.

Well, sorry to hear that. Implicit instantiation is one of the cornerstones of compile-time polymorphism in C++. Sure, it complicates the language and demands quite some intelligence from the compiler, but I believe, it is rather essential.

This was my position for quite some time. I was persuaded by Walter to take a suck-it-and-see approach, which I have. Walter is moved by demonstrations of practical need, not biased opinion. This is why he's now adding member-template functions - because I've demonstrated a need (in MDTL <g>) - and not implicit instantation - because no-one's yet demonstrated it. I think, however, that implicit instantiation, in full form at least, will never get in because it contradicts with the module architecture too much.
 Well - I won't take up fighting for it, right now. If it's really important,
 it probably will keep itching not me but many others as well, until maybe,
 someday we can get someone to scratch...

Apr 25 2004
parent "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
Matthew wrote:
 
 Walter is moved by demonstrations of practical need, not biased opinion. This
is
 why he's now adding member-template functions - because I've demonstrated a
need
 (in MDTL <g>) - and not implicit instantation - because no-one's yet
demonstrated
 it.
 

That's the only way to really do it. If you add every feature everyone wants, your program will be bloated, buggy, and still not done because you're not finished adding the features everyone wants. While there are some things I'd like to see in D... namely, default values for arguments, they are by far minor... by far, it addresses most of the large problems with C.. and I don't like OOP that much anyway (not the way people imho overuse it, at least as I see it..) so I'm going to be more than happy with it when we have a few more libraries - ie. DTL. It's often hard on forums to tell that people appreciate all the changes made, and are just saying that they'd like even more frosting on the cake. But sometimes features could be really useful. It's hard to tell unless you see a good guage of their practical application, not just a lot of community support. If this wasn't true, we'd have mng support in Firefox/Mozilla by now, eh? But in truth, mngs are not all that useful for the web, and would only be marginally useful for xul - but in most cases animated things in the interface are annoying anyway. -[Unknown]
Apr 25 2004
prev sibling parent Andy Friesen <andy ikagames.com> writes:
Norbert Nemec wrote:

 Hi there,
 
 I would like to be able to do something like:
 
---------------------------

class receiver { void someroutine(int i) { dosomething(i); } }; mysignal.connect(slot(&receiver.someroutine)); <--------------------------- Anyhow, what I actually have to do, is something ugly, like:
---------------------------

mysignal.connect(slot!(void delegate(int)).slot(&receiver.someroutine)); <--------------------------- Any idea how I might so something similar to implicit function template instantiation in C++?

You're hosed. :) D doesn't do any sort of implicit type deduction on templates. It's my understanding that this simplifies the compiler considerably. You could mitigate the problem by doing something like this: interface Slot(T) { void opCall(T t); } class DelegateSlot(T) : Slot!(T) { alias void delegate(T) Func; private Func _func; this(Func f) { _func = f; } void opCall(T t) { _func(t); } } class Signal(T) { void opCall(T t) { ... } void connect(void delegate(T) del) { connect(new DelegateSlot!(T)(del)); } void connect(Slot!(T) s) { connect(s); } } then just do: mysignal.connect(&reciever.method); -- andy
Apr 25 2004