Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 09, 2011 Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Is it possible to create a template which only accepts functions or delegates like this example: class Example(T : void function()) { // or ..(T : void delegate()).. T callback; } Where T is a function or a delegate... Thanks for every suggestion! |
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | On Wed, 09 Feb 2011 14:35:42 -0500, useo <useo@start.bg> wrote:
> Is it possible to create a template which only accepts functions or
> delegates like this example:
>
> class Example(T : void function()) { // or ..(T : void delegate())..
>
> T callback;
>
> }
>
> Where T is a function or a delegate...
>
> Thanks for every suggestion!
class Example(T) if (is(T == delegate) || is(T == function))
{
T callback;
}
-Steve
|
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | == Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel > On Wed, 09 Feb 2011 14:35:42 -0500, useo <useo@start.bg> wrote: > > Is it possible to create a template which only accepts functions or > > delegates like this example: > > > > class Example(T : void function()) { // or ..(T : void delegate ()).. > > > > T callback; > > > > } > > > > Where T is a function or a delegate... > > > > Thanks for every suggestion! > class Example(T) if (is(T == delegate) || is(T == function)) > { > T callback; > } > -Steve Wow, works great - THANKS :) |
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | == Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel > On Wed, 09 Feb 2011 14:35:42 -0500, useo <useo@start.bg> wrote: > > Is it possible to create a template which only accepts functions or > > delegates like this example: > > > > class Example(T : void function()) { // or ..(T : void delegate ()).. > > > > T callback; > > > > } > > > > Where T is a function or a delegate... > > > > Thanks for every suggestion! > class Example(T) if (is(T == delegate) || is(T == function)) > { > T callback; > } > -Steve Is there any chance to do the same for methods like: void example(T)() { } ... something like: void bindEvent(T)(if (is(T == delegate) || is(T == function)))()... |
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | On Wed, 09 Feb 2011 14:59:33 -0500, useo <useo@start.bg> wrote:
> == Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel
>> On Wed, 09 Feb 2011 14:35:42 -0500, useo <useo@start.bg> wrote:
>> > Is it possible to create a template which only accepts functions
> or
>> > delegates like this example:
>> >
>> > class Example(T : void function()) { // or ..(T : void delegate
> ())..
>> >
>> > T callback;
>> >
>> > }
>> >
>> > Where T is a function or a delegate...
>> >
>> > Thanks for every suggestion!
>> class Example(T) if (is(T == delegate) || is(T == function))
>> {
>> T callback;
>> }
>> -Steve
>
> Is there any chance to do the same for methods like:
>
> void example(T)() {
> }
>
> ... something like:
>
> void bindEvent(T)(if (is(T == delegate) || is(T == function)))()...
Yes, but you have the order mixed up.
void bindEvent(T)() if (is...)
In general form, a template constraint goes after the declaration, but before the body of the template.
-Steve
|
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | useo:
> ... something like:
>
> void bindEvent(T)(if (is(T == delegate) || is(T == function)))()...
void foo(T)(T x) if (is(T == delegate) || is(T == function)) { ... }
Bye,
bearophile
|
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | == Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel > On Wed, 09 Feb 2011 14:59:33 -0500, useo <useo@start.bg> wrote: > > == Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel > >> On Wed, 09 Feb 2011 14:35:42 -0500, useo <useo@start.bg> wrote: > >> > Is it possible to create a template which only accepts functions > > or > >> > delegates like this example: > >> > > >> > class Example(T : void function()) { // or ..(T : void delegate > > ()).. > >> > > >> > T callback; > >> > > >> > } > >> > > >> > Where T is a function or a delegate... > >> > > >> > Thanks for every suggestion! > >> class Example(T) if (is(T == delegate) || is(T == function)) > >> { > >> T callback; > >> } > >> -Steve > > > > Is there any chance to do the same for methods like: > > > > void example(T)() { > > } > > > > ... something like: > > > > void bindEvent(T)(if (is(T == delegate) || is(T == function))) ()... > Yes, but you have the order mixed up. > void bindEvent(T)() if (is...) > In general form, a template constraint goes after the declaration, but > before the body of the template. > -Steve Ah, okay... I already tried the if-statement after the declaration but I always got an error like this: "... semicolon expected following function declaration" I just removed ( and ) from the if-statement and it's working, thanks in again! |
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | I just have a problem with my variables. For example... my class/template just looks like: class Example(T) if (is(T == delegate) || is(T == function)) { T callback; void setCallback(T cb) { callback = cb; } } This means that I need variables like Example!(void function()) myVariable. But is there any possibility to use variables like Example myVariable? The template declaration only defines the type of a callback and perhaps one method-declaration nothing else. I already tried Example!(void*) because delegates and functions are void pointers but I always get an error. I hope there is any way to do this. |
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | On Wed, 09 Feb 2011 16:14:04 -0500, useo <useo@start.bg> wrote:
> I just have a problem with my variables.
>
> For example... my class/template just looks like:
>
> class Example(T) if (is(T == delegate) || is(T == function))
> {
> T callback;
>
> void setCallback(T cb) {
> callback = cb;
> }
>
> }
>
> This means that I need variables like Example!(void function())
> myVariable. But is there any possibility to use variables like
> Example myVariable? The template declaration only defines the type of
> a callback and perhaps one method-declaration nothing else. I already
> tried Example!(void*) because delegates and functions are void
> pointers but I always get an error. I hope there is any way to do
> this.
If I understand you correctly, you don't want to declare the type of T when instantiating the template? This is not possible, templates must have all parameters defined at instantiation time.
If you just want a shorter thing to type for Example!(void function()), you can do:
alias Example!(void function()) MyType;
MyType myVariable;
-Steve
|
February 09, 2011 Re: Template for function or delegate (nothing else) | ||||
---|---|---|---|---|
| ||||
Posted in reply to useo | useo: > I just have a problem with my variables. > > For example... my class/template just looks like: > > class Example(T) if (is(T == delegate) || is(T == function)) > { > T callback; > > void setCallback(T cb) { > callback = cb; > } > > } > > This means that I need variables like Example!(void function()) > myVariable. But is there any possibility to use variables like > Example myVariable? D is not the SML language, templates are just placeholders. If you don't instantiate a template, you have only a symbol. Example is only assignable to an alias (and in past, to a typedef): alias Example Foo; > The template declaration only defines the type of > a callback and perhaps one method-declaration nothing else. I already > tried Example!(void*) because delegates and functions are void > pointers but I always get an error. I hope there is any way to do > this. I don't yet understand what you are trying to do. Other notes: - What if your T is a functor (a callable class/struct/union instance that defined opCall)? - sizeof of a function pointer is 1 CPU word, while a delegate is 2 CPU words (and a delegate clojure has stuff on the heap too, sometimes). Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation