November 21, 2012
> storage classes on function parameters affect the type of the function, but
> they do not affect the type of the parameter. If a function has a parameter
> named foo, then typeof(foo) is going to be the same whether ref is on it or
> not.
>
> - Jonathan M Davis

Ok. But how to pass storage class to template?
November 21, 2012
On Wed, 21 Nov 2012 12:32:24 -0000, Regan Heath <regan@netmail.co.nz> wrote:

> On Wed, 21 Nov 2012 12:30:08 -0000, Regan Heath <regan@netmail.co.nz> wrote:
>
>> On Wed, 21 Nov 2012 12:02:45 -0000, Jack Applegame <japplegame@gmail.com> wrote:
>>
>>> void foo(ref int a) { a--; }
>>> struct functor(A...) {
>>>   void function(A) functor;
>>> }
>>> functor!int f;    // functor!(ref int) - wrong
>>> f.functor = &foo; // Error: cannot implicitly convert expression (& foo) of type void function(ref int a) to void function(int)
>>
>> Hmm.. well I got past your initial problem, but I have a new one..
>>
>> alias void function(ref int) funcType;
>>
>> void foo(ref int a) { a--; }
>>
>> struct functor(A...) {
>>    A func;
>> }
>> void main()
>> {
>>    functor!funcType f;
>>    f.func = &foo; //Error: f.func is not an lvalue
>> }
>
> Just realised what is happening here. "f.func = " is trying to call the function, and assign &foo to the "void" result.

Or not, duh! "A..." grr.

alias void function(ref int) funcType;

void foo(ref int a) { a--; }

struct functor(A...) {
  A[0] func;
}
void main()
{
  functor!funcType f;
  f.func = &foo;
}

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
November 21, 2012
This problem appears also in std.signals.
There is no possibility to use functions with ref parameters as signal handler.

import std.signals;

class Foo {
  mixin Signal!(int);
}
class Bar {
  void handler(ref int a) {}
}

void main() {
  Foo foo = new Foo;
  Bar bar = new Bar;
  foo.connect(&bar.handler);
}

outputs:
Error: function signals.Foo.Signal!(int).connect (void delegate(int) slot) is not callable using argument types (void delegate(ref int a))|
Error: cannot implicitly convert expression (&bar.handler) of type void delegate(ref int a) to void delegate(int)


November 21, 2012
> alias void function(ref int) funcType;
>
> void foo(ref int a) { a--; }
>
> struct functor(A...) {
>   A[0] func;
> }
> void main()
> {
>   functor!funcType f;
>   f.func = &foo;
> }

I think the best way is using @property.

alias void function(ref int) funcType;
void foo(ref int a) { a--; }

struct functor(A...) {
  @property void func(A fp) { m_func = fp; }
  A m_func;
}
void main()
{
  functor!funcType f;
  f.func = &foo;
}

November 21, 2012
On 11/21/12, Jack Applegame <japplegame@gmail.com> wrote:
> This problem appears also in std.signals.
> There is no possibility to use functions with ref parameters as
> signal handler.
>
> import std.signals;
>
> class Foo {
>    mixin Signal!(int);
> }
> class Bar {
>    void handler(ref int a) {}
> }
>
> void main() {
>    Foo foo = new Foo;
>    Bar bar = new Bar;
>    foo.connect(&bar.handler);
> }

Known problem, no known proposals or solutions. But there are workarounds, e.g. if you're writing your own signals implementation you could use a function type instead of a lone type, e.g.:

mixin Signal!(void function(ref int));
November 21, 2012
Ugly C++ like template and mixin magic solution:

struct r(T){ alias T type;}
template str(alias A) if(is(A == r!(A.type))) { const char[] str = "ref " ~ A.type.stringof; }
template str(T) { const char[] str = T.stringof; }
template str(T, A...) { const char[] str = str!T ~ ", " ~ str!A; }

struct functor(A...) {
  void opAssign(C)(C callable) { m_fp = callable; }
  void opCall(R...)(auto ref R r) { m_fp(r); }
  private {
    mixin("void function(" ~ str!A ~ ") m_fp;");
  }
}

void foo(ref int a, int b) { a += b; }

void main()
{
  functor!(r!int, int) f;
  f = &foo;
  int a = 1, b = 2;
  f(a, b);
  assert(a == 3);
}

1 2
Next ›   Last »