Jump to page: 1 2
Thread overview
Implicit function template instatiation?
Apr 25, 2004
Norbert Nemec
Apr 25, 2004
J Anderson
Apr 25, 2004
J Anderson
Apr 25, 2004
Norbert Nemec
Apr 25, 2004
J Anderson
Apr 25, 2004
Ivan Senji
Apr 25, 2004
J Anderson
Apr 25, 2004
Norbert Nemec
Apr 25, 2004
Matthew
Apr 25, 2004
Andy Friesen
April 25, 2004
Hi there,

I have something like:

>--------------------------
class Slot(T) {
        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:

>---------------------------
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++?

Ciao,
Nobbi

April 25, 2004
Norbert Nemec wrote:

>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++?
>
>Ciao,
>Nobbi
>  
>
What about something like:

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

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


?

-- 
-Anderson: http://badmama.com.au/~anderson/
April 25, 2004
J Anderson wrote:

>>
> What about something like:
>
> 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/
April 25, 2004
J Anderson wrote:

> J Anderson wrote:
> 
>>>
>> What about something like:
>>
>> 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.
April 25, 2004
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/
April 25, 2004
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
April 25, 2004
"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. -
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

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/


April 25, 2004
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...
April 25, 2004
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 :)
>  
>
Yeah, your probably right (I also have asked for implicit before). 

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/
April 25, 2004
"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...


« First   ‹ Prev
1 2