digitalmars.D.learn - initializer for array of function literals
- Adrian Matoga (9/9) Oct 30 2010 Hello,
- bearophile (11/18) Oct 30 2010 When you ask for questions like these, please if possible show a complet...
- Adrian Matoga (6/24) Oct 31 2010 Sorry, I didn't included main() just because an empty one was enough.
- spir (30/53) Oct 31 2010 compilable program that includes a main().
- bearophile (5/7) Oct 31 2010 In D1 there were delegates (fat pointers) but not closures. So the name ...
- Don (2/18) Oct 30 2010 I think that's a bug. I can't think of any reason why that shouldn't wor...
- bearophile (2/3) Oct 31 2010 http://d.puremagic.com/issues/show_bug.cgi?id=5143
- spir (16/21) Oct 31 2010 rk.
Hello, I would appreciate if somebody explained to me why this code: static void function(int a)[] foo = [ function (int a) { } ]; causes the following compile error: test.d(2): Error: non-constant expression __funcliteral1 (DMD 2.050, Windows7) TIA -- Adrian
Oct 30 2010
Adrian Matoga:I would appreciate if somebody explained to me why this code: static void function(int a)[] foo = [ function (int a) { } ]; causes the following compile error: test.d(2): Error: non-constant expression __funcliteral1When you ask for questions like these, please if possible show a complete compilable program that includes a main(). This doesn't compile: static void function(int a)[] foo = [function (int a) {}]; void main() {} This works, but I don't know/remember what's the difference: void f1(int a) {} static void function(int a)[] foo = [&f1]; void main() {} Bye, bearophile
Oct 30 2010
Sorry, I didn't included main() just because an empty one was enough. I know it can be done with pointers to static functions, but I'd prefer syntax with array of function literals for better readability in one particular case I'm playing with now. Anyway, thanks. On 2010-10-31 03:13, bearophile wrote:Adrian Matoga:I would appreciate if somebody explained to me why this code: static void function(int a)[] foo = [ function (int a) { } ]; causes the following compile error: test.d(2): Error: non-constant expression __funcliteral1When you ask for questions like these, please if possible show a complete compilable program that includes a main(). This doesn't compile: static void function(int a)[] foo = [function (int a) {}]; void main() {} This works, but I don't know/remember what's the difference: void f1(int a) {} static void function(int a)[] foo = [&f1]; void main() {} Bye, bearophile
Oct 31 2010
On Sat, 30 Oct 2010 23:13:18 -0400 bearophile <bearophileHUGS lycos.com> wrote:Adrian Matoga: =20compilable program that includes a main().I would appreciate if somebody explained to me why this code: =20 static void function(int a)[] foo =3D [ function (int a) { } ]; =20 causes the following compile error: =20 test.d(2): Error: non-constant expression __funcliteral1=20 When you ask for questions like these, please if possible show a complete==20 This doesn't compile: =20 static void function(int a)[] foo =3D [function (int a) {}]; void main() {} =20 =20 This works, but I don't know/remember what's the difference: =20 void f1(int a) {} static void function(int a)[] foo =3D [&f1]; void main() {}(Note: no need to name parameters in func types.) Upon func literals, to, I don't understand why this doesn't compile: int hof(int function(int i) f , int i) {return f(i) ;} void main () { writeln( hof((int i) {return ++i ;} , 1) ); } Element.d(15): Error: function Element.hof (int function(int i) mapFunc, in= t i) is not callable using argument types (int delegate(int i),int) Element.d(15): Error: cannot implicitly convert expression (__dgliteral1) o= f type int delegate(int i) to int function(int i) According to "the D programming language", D is supposed to construct a fun= ction when the definition does not use any variable of its environment (rea= d: if the closure holds no upvalue); as opposed a so-called delegate (*). I= n this case, it's clear that "(int i) {return ++i ;}" does not close over a= nything, isn't it? So why does D construct a delegate anyway? Also, I think D should not annoy us with the func/delegate distinction -- w= hich is only for saving space -- and accept func defs that match the given = type signature. The annoying distinction is semantically irrelevant; the si= gnature is relevant. Denis (*) By the way, why does D call closures "delegates"? I find this very misl= eading, since delegation already has some meaning in the context of program= ming. And well, "closure" is well established precisely in this sense. Some= times, PL designers amaze me ;-) -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Oct 31 2010
spir:Also, I think D should not annoy us with the func/delegate distinction -- which is only for saving space -- and accept func defs that match the given type signature. The annoying distinction is semantically irrelevant; the signature is relevant.A system language (that supports inline asm too, as D) must allow you to make your choices on practical (implementation) basis too.(*) By the way, why does D call closures "delegates"? I find this very misleading, since delegation already has some meaning in the context of programming. And well, "closure" is well established precisely in this sense. Sometimes, PL designers amaze me ;-)In D1 there were delegates (fat pointers) but not closures. So the name distinction was correct. Later closures were added, but the name didn't change. Bye, bearophile
Oct 31 2010
Adrian Matoga wrote:Hello, I would appreciate if somebody explained to me why this code: static void function(int a)[] foo = [ function (int a) { } ]; causes the following compile error: test.d(2): Error: non-constant expression __funcliteral1 (DMD 2.050, Windows7) TIA -- AdrianI think that's a bug. I can't think of any reason why that shouldn't work.
Oct 30 2010
Don:I think that's a bug. I can't think of any reason why that shouldn't work.http://d.puremagic.com/issues/show_bug.cgi?id=5143
Oct 31 2010
On Sun, 31 Oct 2010 09:16:55 -0400 bearophile <bearophileHUGS lycos.com> wrote:Don: =20rk.I think that's a bug. I can't think of any reason why that shouldn't wo==20 http://d.puremagic.com/issues/show_bug.cgi?id=3D5143The bug report states an issue about func arrays, but: // ok void f (int a) {}; static void function(int) foo =3D &f; // not ok static void function(int) foo =3D function void(int a) {}; Error: non-constant expression __funcliteral1 It seems to me the issue is that one cannot init a func variable with a fun= c literal. Array or not. What does the compiler expect there? Deniq -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Oct 31 2010