www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function literal bug?

reply "Sergei Nosov" <sergei.nosov gmail.com> writes:
Hi!

This is kind of bug report/question.

I'm running Ubuntu 12.04 (x64), DMD v2.064.2 and have the 
following code:

T identity(T)(T e) { return e; }
struct S(alias Func)
{
     void call()
     {
         import std.stdio;
         writeln(Func("string").length);
     }
}
static struct S1
{
     alias S!(identity) A1;
     //alias S!(x => x) A2;
     alias S!(function string (string e) { return e; }) A3;
}
void main()
{
     S1.A1.init.call();
     S1.A3.init.call();
}

The main complaint is that function literal is somehow broken in 
that case. The output of the program is
6
4527264
For some reason, length in the second case is wrong.

Another question (which I believe is reasonable) is that I cannot 
use the (x => x) syntax for function literal (the commented alias 
A2). The compilation error is:

tmp.d(15): Error: delegate tmp.S1.__lambda4!string.__lambda4 
function literals cannot be class members
tmp.d(8): Error: template instance tmp.S1.__lambda4!string error 
instantiating
tmp.d(15):        instantiated from here: S!((x) => x)
tmp.d(8): Error: this for __lambda4 needs to be type S1 not type 
S!((x) => x)

I assume, compiler infers the type of the literal to be delegate, 
but it's clearly not the case, since I don't need no context here.

I appreciate any comments.
Nov 27 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Sergei Nosov:

 T identity(T)(T e) { return e; }
 struct S(alias Func)
 {
     void call()
     {
         import std.stdio;
         writeln(Func("string").length);
     }
 }
 static struct S1
 {
     alias S!(identity) A1;
     //alias S!(x => x) A2;
     alias S!(function string (string e) { return e; }) A3;
 }
 void main()
 {
     S1.A1.init.call();
     S1.A3.init.call();
 }

 The main complaint is that function literal is somehow broken 
 in that case. The output of the program is
 6
 4527264
 For some reason, length in the second case is wrong.
Global structs don't need the "static" attribute. This version of your code gives the output 6 6 on Windows 32 bit: import std.stdio; T identity(T)(T e) { return e; } struct S(alias Func) { void call() { Func("string").length.writeln; } } struct S1 { alias A1 = S!identity; //alias A2 = S!(x => x); alias A3 = S!(function string(string s) => s); } void main() { S1.A1.init.call; S1.A3.init.call; } I don't know why in A2 it infers a delegate. Bye, bearophile
Nov 28 2013
parent reply "Sergei Nosov" <sergei.nosov gmail.com> writes:
On Thursday, 28 November 2013 at 08:23:22 UTC, bearophile wrote:
 Global structs don't need the "static" attribute.
I've thought so, but added it just as a "I really mean, that I don't need context".
 This version of your code gives the output 6 6 on Windows 32 
 bit:
Do you have a 64-bit OS at hand?
 I don't know why in A2 it infers a delegate.
There's at least 2 of us.
Nov 28 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Sergei Nosov:

 Do you have a 64-bit OS at hand?
On 64 bit using dmd 2.064.2 it doesn't give the 6 6 output, so it seems a 64 bit bug. This happens because the 64 bit version of dmd is quite more new than the 32 bit version, so it has more bugs. Bye, bearophile
Nov 28 2013
parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
On Thursday, 28 November 2013 at 09:48:47 UTC, bearophile wrote:
 Sergei Nosov:

 Do you have a 64-bit OS at hand?
On 64 bit using dmd 2.064.2 it doesn't give the 6 6 output, so it seems a 64 bit bug. This happens because the 64 bit version of dmd is quite more new than the 32 bit version, so it has more bugs. Bye, bearophile
It's a known front-end issue. https://d.puremagic.com/issues/show_bug.cgi?id=11545 Kenji Hara
Nov 28 2013
parent "Sergei Nosov" <sergei.nosov gmail.com> writes:
On Thursday, 28 November 2013 at 10:23:39 UTC, Kenji Hara wrote:
 It's a known front-end issue.

 https://d.puremagic.com/issues/show_bug.cgi?id=11545

 Kenji Hara
Great! Does this pull resolve both issues? (correct length and x=>x syntax)
Nov 28 2013