www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - function literals cannot be class members

--0016e6de049b17e0cc04a82cddcd
Content-Type: text/plain; charset=ISO-8859-1

Greetings

I have experienced that it becomes quite difficult to instantiate certain
template structs/classes as members of another class/struct, when
function/delegate need to be passed as a template parameter to the
instantiated templates. For example if I wish to instantiate a binary heap,
and I want to pass a complicated comparison function, the semantics force me
to make the heap instantiation inside a class function. This happens because
when the heap is instantiated as a class member and I try passing a delegate
literal as the comparison operator, the compiler construes the delegate
literal to be a class member which it does not allow.

For example, look at the following code. Since the delegate literal is
deemed to be class member, this code not allowed. This forces me to move the
BinaryHeap instance inside a member function like "bar" in the code. As a
result I loose the capability to access this heap instance from other
functions like frop, since it now is inside a function scope and is no
longer a member of the class object.

// file heap.d -- this code does not compile
import std.container;

class Foo {
  bool signed = false;
  BinaryHeap!(uint, delegate (uint a, uint b) {if(signed) a > b; else b >
a;}) heap;

  void bar () {/* uses heap */}
  void frop () {/* uses heap */}
}

void main() {
  Foo foo;
}

// end of file

Can not this limitation be overcome by creating a special local scope for
such delegate literals? Is something to this effect planned?

Or is there any obvious existing solution I am missing here?

Regards
- Puneet

--0016e6de049b17e0cc04a82cddcd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Greetings<div><br></div><div>I have experienced that it becomes quite diffi=
cult to=A0instantiate=A0certain template structs/classes as members of anot=
her class/struct, when function/delegate need to be passed as a template pa=
rameter to the instantiated templates. For example if I wish to instantiate=
 a binary heap, and I want to pass a complicated comparison function, the s=
emantics force me to make the heap instantiation inside a class function. T=
his happens because when the heap is instantiated as a class member and I t=
ry passing a delegate literal as the comparison operator, the compiler cons=
trues the delegate literal to be a class member which it does not allow.</d=
iv>

<div><br></div><div>For example, look at the following code. Since the dele=
gate literal is deemed to be class member, this code not allowed. This forc=
es me to move the BinaryHeap instance inside a member function like &quot;b=
ar&quot; in the code. As a result I loose the capability to access this hea=
p instance from other functions like frop, since it now is inside a functio=
n scope and is no longer a member of the class object.</div>

<div><br></div><div>// file heap.d -- this code does not compile</div><div>=
<div>import std.container;</div><div><br></div><div>class Foo {</div><div>=
=A0 bool signed =3D false;</div><div>=A0 BinaryHeap!(uint, delegate (uint a=
, uint b)=A0{if(signed) a &gt; b; else b &gt; a;}) heap;</div>

<div><br></div><div>=A0=A0void bar () {/* uses heap */}</div><div>=A0=A0voi=
d frop () {/* uses heap */}</div><div>}</div><div><br></div><div>void main(=
) {</div><div>=A0 Foo foo;</div><div>}</div></div><div><br></div><div>// en=
d of file</div>

<div><br></div><div>Can not this limitation be overcome by creating a speci=
al local scope for such delegate literals? Is something to this effect plan=
ned?</div><div><br></div><div>Or is there any obvious existing solution I a=
m missing here?</div>

<div><br></div><div>Regards</div><div>- Puneet</div><div><br></div>

--0016e6de049b17e0cc04a82cddcd--
Jul 16 2011