Thread overview |
---|
November 28, 2008 Internal delegate and Stack Overflow | ||||
---|---|---|---|---|
| ||||
Hello, I would do something like this, but this return me an execution error : object.Exception: Stack Overflow // --------CODE-------- class A { void delegate() dg; void doIt() { dg(); } } class B { A a; this() { a = new A; a.dg = { doSomething(); }; } void doSomething() { } } void main() { auto b = new B; b.a.doIt(); } // ------END CODE------ Is this a bug or have I do something wrong ? Thanks in advance for your help, TSalm |
November 29, 2008 Re: Internal delegate and Stack Overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsalm | Reply to TSalm,
> Hello,
>
> I would do something like this, but this return me an execution error
> : object.Exception: Stack Overflow
>
> // --------CODE--------
> class A
> {
> void delegate() dg;
> void doIt()
> {
> dg();
> }
> }
> class B
> {
> A a;
> this()
> {
> a = new A;
> a.dg = { doSomething(); };
> }
> void doSomething() { }
> }
> void main()
> {
> auto b = new B;
> b.a.doIt();
> }
> // ------END CODE------
>
> Is this a bug or have I do something wrong ?
> Thanks in advance for your help,
> TSalm
If this is d1.0 the error is that you are allowing an anon delegate to escape the enclosing function.
a.dg = &this.doSomething; // this would be ok if that helps.
if it is 2.0, I think this is correct.
|
November 29, 2008 Re: Internal delegate and Stack Overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | Le Sat, 29 Nov 2008 01:08:28 +0100, BCS <ao@pathlink.com> a écrit: > Reply to TSalm, > >> Hello, >> I would do something like this, but this return me an execution error >> : object.Exception: Stack Overflow >> // --------CODE-------- >> class A >> { >> void delegate() dg; >> void doIt() >> { >> dg(); >> } >> } >> class B >> { >> A a; >> this() >> { >> a = new A; >> a.dg = { doSomething(); }; >> } >> void doSomething() { } >> } >> void main() >> { >> auto b = new B; >> b.a.doIt(); >> } >> // ------END CODE------ >> Is this a bug or have I do something wrong ? >> Thanks in advance for your help, >> TSalm > > If this is d1.0 the error is that you are allowing an anon delegate to escape the enclosing function. Yes, it's on D1.036. > > a.dg = &this.doSomething; // this would be ok if that helps. > > > if it is 2.0, I think this is correct. > Yes, you are right. But this is an example code. The "true" code uses delegates with argument which differs from called functions, so I can't point delegate directly to them. And this anonymous function's way is really fastest to code... I must waiting for a D2 stable version ;-) Thanks |
November 29, 2008 Re: Internal delegate and Stack Overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsalm | Reply to TSalm,
> Yes, you are right.
> But this is an example code. The "true" code uses delegates with
> argument
> which differs from called functions, so I can't point delegate
> directly to
> them.
> And this anonymous function's way is really fastest to code... I must
> waiting for a D2 stable version ;-)
> Thanks
>
you can store off the values you need like this:
struct C(R, A...)
{
A args;
R function(A) dg;
static R delegate() opCall(R function(A) dg, A a)
{
C!(R, A) ret;
ret.dg=dg;
foreach(int i,_;A)
ret.args[i] = a[i];
return &ret.fn;
}
R fn() { return dg(args); }
}
// test it
import std.stdio;
int delegate() dg(int i)
{
return C!(int,int)(function int(int j){return j;}, i); // <--- used here
}
void main()
{
auto d = dg(5);
writef("%s\n", d());
}
|
November 29, 2008 Re: Internal delegate and Stack Overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | Le Sat, 29 Nov 2008 01:49:20 +0100, BCS <ao@pathlink.com> a écrit:
> struct C(R, A...)
> {
> A args;
> R function(A) dg;
> static R delegate() opCall(R function(A) dg, A a)
> {
> C!(R, A) ret;
> ret.dg=dg;
> foreach(int i,_;A)
> ret.args[i] = a[i];
> return &ret.fn;
> }
> R fn() { return dg(args); }
> }
> // test it
> import std.stdio;
> int delegate() dg(int i)
> {
> return C!(int,int)(function int(int j){return j;}, i); // <--- used here
> }
> void main()
> {
> auto d = dg(5);
> writef("%s\n", d());
> }
Interesting code. Thanks!
|
Copyright © 1999-2021 by the D Language Foundation