www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - filter!(not!(predicate))(someInputRange) does not compile

reply Jens Mueller <jens.k.mueller gmx.de> writes:
Hi,

I cannot make the following compile.

import std.functional;
import std.array;
import std.algorithm;
import std.stdio;

void main() {
    auto numbers = [0, 1, 2, 3, 4, 5];

    bool alwaysTrue(uint a) { return true; }
    alias not!(alwaysTrue) alwaysFalse;

    numbers = array(filter!(alwaysTrue)(numbers));
    writeln(numbers);
    numbers = array(filter!(alwaysFalse)(numbers)); // does not compile
    writeln(numbers);
}

The line with alwaysFalse fails with:
/path/to/../src/phobos/std/algorithm.d(854): Error: constructor
std.algorithm.Filter!(not,int[]).Filter.this cannot get frame pointer to
not

Any ideas what I'm doing wrong or workarounds?

Jens
Jan 19 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/19/11 5:53 PM, Jens Mueller wrote:
 Hi,

 I cannot make the following compile.

 import std.functional;
 import std.array;
 import std.algorithm;
 import std.stdio;

 void main() {
      auto numbers = [0, 1, 2, 3, 4, 5];

      bool alwaysTrue(uint a) { return true; }
      alias not!(alwaysTrue) alwaysFalse;

      numbers = array(filter!(alwaysTrue)(numbers));
      writeln(numbers);
      numbers = array(filter!(alwaysFalse)(numbers)); // does not compile
      writeln(numbers);
 }

 The line with alwaysFalse fails with:
 /path/to/../src/phobos/std/algorithm.d(854): Error: constructor
 std.algorithm.Filter!(not,int[]).Filter.this cannot get frame pointer to
 not

 Any ideas what I'm doing wrong or workarounds?

 Jens
Place the call to not!alwaysTrue in a local function inside main: bool alwaysFalse(uint a) { return not!alwaysTrue(a); } Andrei
Jan 19 2011
next sibling parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
Andrei Alexandrescu wrote:
 On 1/19/11 5:53 PM, Jens Mueller wrote:
Hi,

I cannot make the following compile.

import std.functional;
import std.array;
import std.algorithm;
import std.stdio;

void main() {
     auto numbers = [0, 1, 2, 3, 4, 5];

     bool alwaysTrue(uint a) { return true; }
     alias not!(alwaysTrue) alwaysFalse;

     numbers = array(filter!(alwaysTrue)(numbers));
     writeln(numbers);
     numbers = array(filter!(alwaysFalse)(numbers)); // does not compile
     writeln(numbers);
}

The line with alwaysFalse fails with:
/path/to/../src/phobos/std/algorithm.d(854): Error: constructor
std.algorithm.Filter!(not,int[]).Filter.this cannot get frame pointer to
not

Any ideas what I'm doing wrong or workarounds?

Jens
Place the call to not!alwaysTrue in a local function inside main: bool alwaysFalse(uint a) { return not!alwaysTrue(a); }
Thanks. Can you elaborate a bit please? I wonder why the alias won't work. Jens
Jan 19 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/19/11 7:19 PM, Jens Mueller wrote:
 Andrei Alexandrescu wrote:
 On 1/19/11 5:53 PM, Jens Mueller wrote:
 Hi,

 I cannot make the following compile.

 import std.functional;
 import std.array;
 import std.algorithm;
 import std.stdio;

 void main() {
      auto numbers = [0, 1, 2, 3, 4, 5];

      bool alwaysTrue(uint a) { return true; }
      alias not!(alwaysTrue) alwaysFalse;

      numbers = array(filter!(alwaysTrue)(numbers));
      writeln(numbers);
      numbers = array(filter!(alwaysFalse)(numbers)); // does not compile
      writeln(numbers);
 }

 The line with alwaysFalse fails with:
 /path/to/../src/phobos/std/algorithm.d(854): Error: constructor
 std.algorithm.Filter!(not,int[]).Filter.this cannot get frame pointer to
 not

 Any ideas what I'm doing wrong or workarounds?

 Jens
Place the call to not!alwaysTrue in a local function inside main: bool alwaysFalse(uint a) { return not!alwaysTrue(a); }
Thanks. Can you elaborate a bit please? I wonder why the alias won't work.
I thought of it for a bit. It's a limitation of the compiler that's worth a bug report. The explanation is a bit involved. Let me start by remarking that if you prepend "static" to alwaysTrue, the alias works as expected: static bool alwaysTrue(uint a) { return true; } alias not!(alwaysTrue) alwaysFalse; Without "static", alwaysTrue has access to a hidden pointer to the stack frame of the function is in. In theory a template should be unable to manipulate alwaysTrue because of that frame pointer. But Walter has had this great idea of instantiating templates in the context of the function they're in, so they gain access to the frame pointer too. However, that instantiation mechanism still has a few limitations. I think the code above runs into one of them. Andrei
Jan 19 2011
parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
Andrei Alexandrescu wrote:
 On 1/19/11 7:19 PM, Jens Mueller wrote:
Andrei Alexandrescu wrote:
Place the call to not!alwaysTrue in a local function inside main:

     bool alwaysFalse(uint a) { return not!alwaysTrue(a); }
Thanks. Can you elaborate a bit please? I wonder why the alias won't work.
I thought of it for a bit. It's a limitation of the compiler that's worth a bug report. The explanation is a bit involved. Let me start by remarking that if you prepend "static" to alwaysTrue, the alias works as expected: static bool alwaysTrue(uint a) { return true; } alias not!(alwaysTrue) alwaysFalse; Without "static", alwaysTrue has access to a hidden pointer to the stack frame of the function is in.
Yeah. That makes sense. It has this hidden pointer to access variables from the function it is defined in.
 In theory a template should be unable to manipulate alwaysTrue
 because of that frame pointer. But Walter has had this great idea of
 instantiating templates in the context of the function they're in,
 so they gain access to the frame pointer too. However, that
 instantiation mechanism still has a few limitations. I think the
 code above runs into one of them.
I see. I can file a bug report if it is considered important and should not be forgotten. Jens
Jan 20 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/20/11 12:47 PM, Jens Mueller wrote:
 Andrei Alexandrescu wrote:
 On 1/19/11 7:19 PM, Jens Mueller wrote:
 Andrei Alexandrescu wrote:
 Place the call to not!alwaysTrue in a local function inside main:

      bool alwaysFalse(uint a) { return not!alwaysTrue(a); }
Thanks. Can you elaborate a bit please? I wonder why the alias won't work.
I thought of it for a bit. It's a limitation of the compiler that's worth a bug report. The explanation is a bit involved. Let me start by remarking that if you prepend "static" to alwaysTrue, the alias works as expected: static bool alwaysTrue(uint a) { return true; } alias not!(alwaysTrue) alwaysFalse; Without "static", alwaysTrue has access to a hidden pointer to the stack frame of the function is in.
Yeah. That makes sense. It has this hidden pointer to access variables from the function it is defined in.
 In theory a template should be unable to manipulate alwaysTrue
 because of that frame pointer. But Walter has had this great idea of
 instantiating templates in the context of the function they're in,
 so they gain access to the frame pointer too. However, that
 instantiation mechanism still has a few limitations. I think the
 code above runs into one of them.
I see. I can file a bug report if it is considered important and should not be forgotten. Jens
Yes please. Make it an enhancement as it would remove a limitation of an otherwise little explored feature. Thanks! Andrei
Jan 20 2011
parent "Jens K. Mueller" <jens.k.mueller gmx.de> writes:
Andrei Alexandrescu wrote:
 On 1/20/11 12:47 PM, Jens Mueller wrote:
Andrei Alexandrescu wrote:
On 1/19/11 7:19 PM, Jens Mueller wrote:
Andrei Alexandrescu wrote:
Place the call to not!alwaysTrue in a local function inside main:

     bool alwaysFalse(uint a) { return not!alwaysTrue(a); }
Thanks. Can you elaborate a bit please? I wonder why the alias won't work.
I thought of it for a bit. It's a limitation of the compiler that's worth a bug report. The explanation is a bit involved. Let me start by remarking that if you prepend "static" to alwaysTrue, the alias works as expected: static bool alwaysTrue(uint a) { return true; } alias not!(alwaysTrue) alwaysFalse; Without "static", alwaysTrue has access to a hidden pointer to the stack frame of the function is in.
Yeah. That makes sense. It has this hidden pointer to access variables from the function it is defined in.
In theory a template should be unable to manipulate alwaysTrue
because of that frame pointer. But Walter has had this great idea of
instantiating templates in the context of the function they're in,
so they gain access to the frame pointer too. However, that
instantiation mechanism still has a few limitations. I think the
code above runs into one of them.
I see. I can file a bug report if it is considered important and should not be forgotten. Jens
Yes please. Make it an enhancement as it would remove a limitation of an otherwise little explored feature. Thanks!
http://d.puremagic.com/issues/show_bug.cgi?id=5469
Jan 20 2011
prev sibling parent spir <denis.spir gmail.com> writes:
On 01/20/2011 02:19 AM, Jens Mueller wrote:
 Thanks. Can you elaborate a bit please? I wonder why the alias won't
 work.
Because in your original version the alias line does not define a func, but a kind of constant symbol. A higher order func like filter expects a func as first arg, not a constant. (my 2 c) denis _________________ vita es estrany spir.wikidot.com
Jan 20 2011