www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - initializer for array of function literals

reply Adrian Matoga <epi atari8.info> writes:
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
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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 __funcliteral1

When 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
next sibling parent Adrian Matoga <epi atari8.info> writes:
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 __funcliteral1

When 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
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
prev sibling next sibling parent reply Don <nospam nospam.com> writes:
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
 
 -- 
 Adrian

I think that's a bug. I can't think of any reason why that shouldn't work.
Oct 30 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
prev sibling parent spir <denis.spir gmail.com> writes:
On Sun, 31 Oct 2010 09:16:55 -0400
bearophile <bearophileHUGS lycos.com> wrote:

 Don:
=20
 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=3D5143

The 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
prev sibling parent spir <denis.spir gmail.com> writes:
On Sat, 30 Oct 2010 23:13:18 -0400
bearophile <bearophileHUGS lycos.com> wrote:

 Adrian Matoga:
=20
 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

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