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

Reply via email to