Hi!
I get the following compile error (linux, dmd2.066.1):
test.d(13): Error: template test.testFunc cannot deduce function from
argument types !()(double[], double), candidates are:
test.d(3):test.testFunc(R)(R range, ElementType!R foo)
For the following test file:
import std.range:
Ali Çehreli wrote:
On 02/10/2015 12:31 AM, ted wrote:
ElementType!R testFunc(R)( R range, ElementType!R foo) //
compiles
with double foo
If think it is a little too much to ask from the template system of D. A
proper way of doing the same thing is to use a template constraint:
On 02/10/2015 12:31 AM, ted wrote:
ElementType!R testFunc(R)( R range, ElementType!R foo) // compiles with
double foo
If think it is a little too much to ask from the template system of D. A
proper way of doing the same thing is to use a template constraint:
ElementType!R testFunc(R,
ted:
Could someone enlighten me ?
This works:
import std.range: ElementType, isInputRange;
ElementType!R testFunc(R, T)(R range, T foo)
if (is(ElementType!R == T)) {
static assert(isInputRange!R);
typeof(return) retVal = foo ^^ 2; // More DRY.
return retVal;
}
void main() {
Ali Çehreli:
If think it is a little too much to ask from the template
system of D.
I remember hitting a similar problem with code like this bar()
function:
// OK
void foo(size_t N1, size_t N2)(int[N1] a, int[N2] b)
if (N2 == N1 ^^ 2) {}
// Not OK
void bar(size_t N)(int[N] a, int[N ^ 2]
void bar(size_t N)(int[N] a, int[N ^ 2] b) {}
I meant:
void bar(size_t N)(int[N] a, int[N ^^ 2] b) {}
bearophile wrote:
ted:
Could someone enlighten me ?
This works:
import std.range: ElementType, isInputRange;
ElementType!R testFunc(R, T)(R range, T foo)
if (is(ElementType!R == T)) {
static assert(isInputRange!R);
typeof(return) retVal = foo ^^ 2; // More DRY.
bearophile wrote:
ted:
... where you say 'More DRY' above, are you referring to
I was referring to both, but mostly to the typeof. It's more DRY
(http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ). You are
stating only once the type of the return variable. This is less
bug-prone.
ted:
... where you say 'More DRY' above, are you referring to
I was referring to both, but mostly to the typeof. It's more DRY
(http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ). You are
stating only once the type of the return variable. This is less
bug-prone.
Bye,
bearophile
On 02/10/2015 01:08 AM, bearophile wrote:
// Not OK
void bar(size_t N)(int[N] a, int[N ^^ 2] b) {}
So perhaps my suggestion to file an enhancement request is not
a good idea...
I am not sure. Although the template system already does pretty clever
deductions, I think they are all based on
10 matches
Mail list logo