function literals cannot be class members

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jul 18 07:48:41 PDT 2011


On 7/18/11 8:21 AM, Steven Schveighoffer wrote:
> On Sun, 17 Jul 2011 09:49:52 -0400, d coder <dlang.coder at gmail.com> wrote:
>> class Foo {
>> BinaryHeap!(uint[], function (a, b) {return a > b;}) heap;
>> // ..
>> }
>>
>> You get another Error: this for __funcliteral1 needs to be type Foo
>> not type
>> BinaryHeap!(uint[],__funcliteral1)
>
> The issue here is that the *type* of the binary heap depends on the
> functor. Yet, you cannot define the functor (which depends on having an
> instance pointer) until you've instantiated the object, which in turn
> requires declaring the type.
>
> What is needed is a BinaryHeap solution that accepts a delegate as a
> parameter instead of a template member. I wonder if there's a reasonable
> mapping from the latter to the former, as I also use alias parameter to
> define functors in dcollections (stolen shamelessly from Andrei's
> design).

We need to keep this key strategic advantage. First off, the fact that 
this code isn't accepted:

BinaryHeap!(uint[], function (a, b) {return a > b;}) heap;

is a clear bug.

> Worst case, BinaryHeap grows a branch which stores/calls a delegate
> instead of a template parameter.

I'll discuss with Walter, but even without any language change we can 
define a special symbol that means "store this object inside". That 
symbol has nothing interesting about it, e.g.:

/// Use this for comparators with state
struct Dynamic(T) {}

Heap!(uint[], Dynamic!(bool delegate(uint, uint))) myHeap;

A better solution have us detect from the alias that we need to allocate 
storage for it in the Heap object.


Andrei


More information about the Digitalmars-d mailing list