digitalmars.D.learn - Is there a way to create compile-time delegates?
- "Simen kjaeraas" <simen.kjaras gmail.com> Jul 19 2010
- torhu <no spam.invalid> Jul 19 2010
- Don <nospam nospam.com> Jul 19 2010
- Sean Kelly <sean invisibleduck.org> Jul 21 2010
- Don <nospam nospam.com> Jul 21 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 19 2010
- "Simen kjaeraas" <simen.kjaras gmail.com> Jul 21 2010
Yeah, what the subject says.
I want to have a default delegate for a struct, and without a default
constructor, this has to be a compile-time constant. Now, logically,
there should be nothing wrong with storing the function pointer and a
null context pointer at compile-time, but it seems there is. Any ideas?
struct foo {
void delegate( ) dg = () {}; // Error: non-constant expression
// __dgliteral1
}
--
Simen
Jul 19 2010
On 19.07.2010 21:06, Simen kjaeraas wrote:Yeah, what the subject says. I want to have a default delegate for a struct, and without a default constructor, this has to be a compile-time constant. Now, logically, there should be nothing wrong with storing the function pointer and a null context pointer at compile-time, but it seems there is. Any ideas? struct foo { void delegate( ) dg = () {}; // Error: non-constant expression // __dgliteral1 }
I wasn't able to make it work. The compiler probably sees delegates as something that just can't be created at compile time, since no runtime contexts exist then. Which is reasonable. Maybe one of those templates that turn functions into delegates will work? Otherwise I guess you're back to using a factory function for initializing instances. Maybe just checking for null pointers before calling those delegates ends up being the easiest solution.
Jul 19 2010
Philippe Sigaud wrote:On Mon, Jul 19, 2010 at 22:01, torhu <no spam.invalid> wrote: I wasn't able to make it work. Me too :( The compiler probably sees delegates as something that just can't be created at compile time, since no runtime contexts exist then. Which is reasonable. Can you initialize pointers in general, at compile-time?
You cannot initialize a pointer to runtime-allocated data at compile-time, and will never be able to. This particular example cannot ever work. You should be able to initialize pointers to static data at compile-time, but currently you can't.Maybe one of those templates that turn functions into delegates will work? I had the same idea and tried to use std.functional.toDelegate, but to no avail. enum moo = () {return 1;}; struct foo { int delegate( ) dg = toDelegate(moo); } Error: forward reference to inferred return type of function call toDelegate(delegate int()| int moo() { return 1;} struct foo { int delegate( ) dg = toDelegate(&moo); } Error: forward reference to inferred return type of function call toDelegate((& moo))| 'auto' strikes again :( I came to hate these forward reference errors. That and the fact that auto functions do not appear in the docs, that's enough for me to avoid auto as much as possible for functions. Otherwise I guess you're back to using a factory function for initializing instances. Maybe just checking for null pointers before calling those delegates ends up being the easiest solution. Philippe
Jul 19 2010
Don Wrote:Philippe Sigaud wrote:On Mon, Jul 19, 2010 at 22:01, torhu <no spam.invalid> wrote: I wasn't able to make it work. Me too :( The compiler probably sees delegates as something that just can't be created at compile time, since no runtime contexts exist then. Which is reasonable. Can you initialize pointers in general, at compile-time?
You cannot initialize a pointer to runtime-allocated data at compile-time, and will never be able to. This particular example cannot ever work.
You can get pretty close by using structs with opCall defined though.
Jul 21 2010
Simen kjaeraas wrote:Sean Kelly <sean invisibleduck.org> wrote:Don Wrote:Philippe Sigaud wrote:On Mon, Jul 19, 2010 at 22:01, torhu <no spam.invalid> wrote: I wasn't able to make it work. Me too :( The compiler probably sees delegates as something that just
created at compile time, since no runtime contexts exist then. Which is reasonable. Can you initialize pointers in general, at compile-time?
You cannot initialize a pointer to runtime-allocated data at compile-time, and will never be able to. This particular example cannot ever work.
You can get pretty close by using structs with opCall defined though.
Not if the function I got only takes delegates, and needs to only take delegates.
points to an immutable static variable. Doesn't work at the moment, though.
Jul 21 2010
--0016e6d9765114ee42048bc37d03 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jul 19, 2010 at 22:01, torhu <no spam.invalid> wrote:I wasn't able to make it work.
Me too :(The compiler probably sees delegates as something that just can't be created at compile time, since no runtime contexts exist then. Which is reasonable.
Can you initialize pointers in general, at compile-time?Maybe one of those templates that turn functions into delegates will work?
I had the same idea and tried to use std.functional.toDelegate, but to no avail. enum moo = () {return 1;}; struct foo { int delegate( ) dg = toDelegate(moo); } Error: forward reference to inferred return type of function call toDelegate(delegate int()| int moo() { return 1;} struct foo { int delegate( ) dg = toDelegate(&moo); } Error: forward reference to inferred return type of function call toDelegate((& moo))| 'auto' strikes again :( I came to hate these forward reference errors. That and the fact that auto functions do not appear in the docs, that's enough for me to avoid auto as much as possible for functions.Otherwise I guess you're back to using a factory function for initializing instances. Maybe just checking for null pointers before calling those delegates ends up being the easiest solution.
Philippe --0016e6d9765114ee42048bc37d03 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Mon, Jul 19, 2010 at 22:01, torhu <span dir= =3D"ltr"><no spam.invalid></span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204= , 204, 204); padding-left: 1ex;"> <div><div></div><div class=3D"h5"><br> <br></div></div> I wasn't able to make it work. =A0</blockquote><div><br>Me too :(<br>= =A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8= ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">The comp= iler probably sees delegates as something that just can't be created at= compile time, since no runtime contexts exist then. =A0Which is reasonable= .<br> </blockquote><div><br>Can you initialize pointers in general, at compile-ti= me?<br><br>=A0<br></div><blockquote class=3D"gmail_quote" style=3D"margin: = 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left:= 1ex;"> Maybe one of those templates that turn functions into delegates will work? = =A0</blockquote><div><br>I had the same idea and tried to use std.functiona= l.toDelegate, but to no avail.<br><br>enum moo =3D ()=A0 {return 1;};<br><b= r> struct foo {<br>=A0=A0 int delegate( ) dg =3D toDelegate(moo);<br>}<br><br>= Error: forward reference to inferred return type of function call toDelegat= e(delegate int()|<br><br>int moo() { return 1;}<br><br>struct foo {<br>=A0= =A0 int delegate( ) dg =3D toDelegate(&moo);<br> }<br><br>Error: forward reference to inferred return type of function call = toDelegate((& moo))|<br><br>'auto' strikes again :(<br>I came t= o hate these forward reference errors. That and the fact that auto function= s do not appear in the docs, that's enough for me to avoid auto as much= as possible for functions.<br> <br>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt= 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Othe= rwise I guess you're back to using a factory function for initializing = instances.<br> <br> Maybe just checking for null pointers before calling those delegates ends u= p being the easiest solution.<br></blockquote><div><br><br>Philippe<br>=A0<= br></div></div><br> --0016e6d9765114ee42048bc37d03--
Jul 19 2010
Sean Kelly <sean invisibleduck.org> wrote:Don Wrote:Philippe Sigaud wrote:On Mon, Jul 19, 2010 at 22:01, torhu <no spam.invalid> wrote: I wasn't able to make it work. Me too :( The compiler probably sees delegates as something that just can't
created at compile time, since no runtime contexts exist then. Which is reasonable. Can you initialize pointers in general, at compile-time?
You cannot initialize a pointer to runtime-allocated data at compile-time, and will never be able to. This particular example cannot ever work.
You can get pretty close by using structs with opCall defined though.
Not if the function I got only takes delegates, and needs to only take delegates. -- Simen
Jul 21 2010









Don <nospam nospam.com> 