independent or parallel process

Ali Çehreli acehreli at yahoo.com
Thu Oct 18 19:25:50 PDT 2012


On 10/18/2012 06:34 PM, drpepper wrote:
 > Thank you Ali,
 >
 > Indeed, I originally had the function outside of main.
 >
 > I have found another strange result with std.concurrency:
 >
 > spawn(&function, array[0].length, array.length);
 > // generates compiling error: "cannot deduce template function
 > from arg types"
 >
 > //Yet,
 > int var_one = array[0].length;
 > int var_two = array.length;
 > spawn(&function, var_one, var_two);
 > // compiles and runs

Reduced code:

void bar(T...)(void function(T) fn, T args)
{}

void foo(size_t i)
{}

void main() {
     bar(&foo, 42);    // <-- compilation error
}

std.concurrency.spawn has the same signature as bar. The problem is with 
that signature: It requires that the arguments must match the parameters 
of the function.

Changing the signature of spawn() to the signature of the following 
bar() should work:

void bar(Func, T...)(Func fn, T args)
     if (__traits(compiles, fn(args)))
{}

void foo(size_t i)
{}

void main() {
     bar(&foo, int.init);      // <-- now compiles
     bar(&foo, size_t.init);
}

I wonder whether others see any weakness in that signature. If not, it 
can be entered to bugzilla as an enhancement request.

Ali



More information about the Digitalmars-d-learn mailing list