February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | "Craig Black" <cblack@ara.com> wrote in message news:cv0bps$11qb$1@digitaldaemon.com... > However, even if we do have a pointer to hold an object that contains the parameters, this still does not solve the problem of having to write a new class for the object. Using a traditional approach, each new method or function signature requires a new data structure to be defined, as well as code that performs the invokation with the parameters in the data structure. With a delegate, you only need to define what the function's input and outputs are. The 'this' part of the delegate is unspecified, and can be any class added on later. > I am seeking a way to define a GENERIC event data structure, perhaps with a > template or a new language feature, that does turns all this coding into a one-liner. > > For example, something like, > > void myFunc(char[] str) > { > // print str to the console, however D does it > } > > // this event will be invoked at t = 1.0, it will print "hey" to the console > Event event = new Event(1.0, myFunc, "hey"); You can do that with delegates: class Event { this(double t, void delegate(char[]) dg, char[] str); } |
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | > You can do that with delegates:
>
> class Event
> {
> this(double t, void delegate(char[]) dg, char[] str);
> }
Yes but what if the function signature changes? Then your Event class does not work and you need another Event class for the other function signature. For example:
void myFunc1(char[] str) { ... }
void myFunc2(int i) { ... }
class Event1
{
double t;
void function(char[]) fun;
char [] str;
this(double _t, void function(char[]) _fun, char[] _str)
{
t = _t;
fun = _fun;
str = _str;
}
void invoke() { fun(str); }
}
class Event2
{
double t;
void function(int) fun;
int i;
this(double _t, void function(char[]) _fun, int _str)
{
t = _t;
fun = _fun;
i = _i;
}
void invoke() { fun(i); }
}
I suppose it would be possible to use templates somehow, but even then you would need different templates for functions/methods with different numbers of parameters. Do you see the problem?
-Craig
|
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> "John Reimer" <brk_6502@yahoo.com> wrote in message
> news:cv0aj4$10jm$1@digitaldaemon.com...
>
>>How do you access object state information from within a delegate? Last
>>I checked, you can't access the 'this' pointer from the delegate.
>
>
> You don't access it directly, you call the delegate, just as you would a
> pointer to a function.
>
>
Yes, I've been confused about this before. I forgot that it would go against the idea of a delegate to have access to the calling objects members.
Kris, you can chide me for asking this question again.
- John R.
|
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | In article <cv0u6q$1qpc$1@digitaldaemon.com>, John Reimer says... > >Walter wrote: >> "John Reimer" <brk_6502@yahoo.com> wrote in message news:cv0aj4$10jm$1@digitaldaemon.com... >> >>>How do you access object state information from within a delegate? Last I checked, you can't access the 'this' pointer from the delegate. >> >> >> You don't access it directly, you call the delegate, just as you would a pointer to a function. >> >> > >Yes, I've been confused about this before. I forgot that it would go against the idea of a delegate to have access to the calling objects members. > >Kris, you can chide me for asking this question again. [chanting] Pie Iesu domine, dona eis requiem. [bonk] Pie Iesu domine,... [bonk] ..dona eis requiem. [bonk] Pie Iesu domine,... [bonk] ..dona eis requiem. |
February 17, 2005 Re: Events [OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | In article <cv0va6$1rn5$1@digitaldaemon.com>, Kris says... > >[chanting] >Pie Iesu domine, dona eis requiem. >[bonk] >Pie Iesu domine,... >[bonk] >..dona eis requiem. >[bonk] >Pie Iesu domine,... >[bonk] >..dona eis requiem. > And now you must chop down the mightiest (mango) tree in the forest... ..with a *herring*! - Eric Anderton at yahoo |
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Kris wrote:
> In article <cv0u6q$1qpc$1@digitaldaemon.com>, John Reimer says...
>
>>Walter wrote:
>>
>>>"John Reimer" <brk_6502@yahoo.com> wrote in message
>>>news:cv0aj4$10jm$1@digitaldaemon.com...
>>>
>>>
>>>>How do you access object state information from within a delegate? Last
>>>>I checked, you can't access the 'this' pointer from the delegate.
>>>
>>>
>>>You don't access it directly, you call the delegate, just as you would a
>>>pointer to a function.
>>>
>>>
>>
>>Yes, I've been confused about this before. I forgot that it would go against the idea of a delegate to have access to the calling objects members.
>>
>>Kris, you can chide me for asking this question again.
>
>
>
> [chanting]
> Pie Iesu domine, dona eis requiem.
> [bonk]
> Pie Iesu domine,...
> [bonk]
> ..dona eis requiem.
> [bonk]
> Pie Iesu domine,...
> [bonk]
> ..dona eis requiem.
>
>
>
Okay... I'm not getting this (other than maybe the [bonk] part :-D ). It seems to be something from a Monty Python script.
|
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | "Craig Black" wrote:
[...]
> Is it possible to know this information at compile time
[...]
It is clearly known at compile time. Seems you want a naked function/delegate formal parameter as well as a signature formal parameter.
<example>
class Event{
this( double time,
delegate dg1, signature s1;
delegate dg2, signature s2){
dg( s);
}
}
// ...
Event e= new Event( 1.0,
&myFunc1, ( actualParm1_1, ..., actualParm1_n),
&myFunc2, ( actualParm2_1, ..., actualParm2_m)
);
</example>
The instruction to the compiler of the formal parameters
`delegate dg, signature s'
would be:
1. at this position expect the reference of a delegate followed by an
actual parameterList surrounded by `(' and `)' (or similar).
2. the list of types of the actual parameter list must match the list
of types of the formal parameter list of the referenced delegate.
-manfred
|
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | In article <cv10tv$1t2s$1@digitaldaemon.com>, John Reimer says... >> [chanting] >> Pie Iesu domine, dona eis requiem. >> [bonk] >> Pie Iesu domine,... >> [bonk] >> ..dona eis requiem. >> [bonk] >> Pie Iesu domine,... >> [bonk] >> ..dona eis requiem. > >Okay... I'm not getting this (other than maybe the [bonk] part :-D ). It seems to be something from a Monty Python script. Self-flagellation (with a heavy book), of the Dominican-Monk variety! Twas indeed from none other than the *Holy Grail!* |
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | You don't need multiple templates... you need templates that take a delegate type as their parameter. Aka:
#
# class Event(alias DgType) {
# this(doublt t, DgType dg, char[] str);
# }
#
Then you would just call for template instances for your signatures. Some examples:
#
# // using the template with a two-param delegate
# alias Event!(void delegate(int,int)) Event_ii;
# Event_ii iievent = new Event_ii(dg);
#
# // using the template with a one-param delegate
# alias Event!(void delegate(char[])) Event_s;
# Event_s event = new Event_s(dg);
#
-- Chris S
Craig Black wrote:
>>You can do that with delegates:
>>
>> class Event
>> {
>> this(double t, void delegate(char[]) dg, char[] str);
>> }
>
>
> Yes but what if the function signature changes? Then your Event class does not work and you need another Event class for the other function signature. For example:
>
> void myFunc1(char[] str) { ... }
> void myFunc2(int i) { ... }
>
> class Event1
> {
> double t;
> void function(char[]) fun;
> char [] str;
> this(double _t, void function(char[]) _fun, char[] _str)
> {
> t = _t;
> fun = _fun;
> str = _str;
> }
> void invoke() { fun(str); }
> }
>
> class Event2
> {
> double t;
> void function(int) fun;
> int i;
> this(double _t, void function(char[]) _fun, int _str)
> {
> t = _t;
> fun = _fun;
> i = _i;
> }
> void invoke() { fun(i); }
> }
>
> I suppose it would be possible to use templates somehow, but even then you would need different templates for functions/methods with different numbers of parameters. Do you see the problem?
>
> -Craig
>
>
|
February 17, 2005 Re: Events | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Kris wrote:
> In article <cv10tv$1t2s$1@digitaldaemon.com>, John Reimer says...
>
>>>[chanting]
>>>Pie Iesu domine, dona eis requiem.
>>>[bonk]
>>>Pie Iesu domine,...
>>>[bonk]
>>>..dona eis requiem.
>>>[bonk]
>>>Pie Iesu domine,...
>>>[bonk]
>>>..dona eis requiem.
>>
>>Okay... I'm not getting this (other than maybe the [bonk] part :-D ). It seems to be something from a Monty Python script.
>
>
> Self-flagellation (with a heavy book), of the Dominican-Monk variety!
>
> Twas indeed from none other than the *Holy Grail!*
>
>
>
:-D ... Good one!
|
Copyright © 1999-2021 by the D Language Foundation