normal function and template function conflict
Simen Kjaeraas
simen.kjaras at gmail.com
Thu Jul 26 11:43:07 PDT 2012
On Thu, 26 Jul 2012 20:14:10 +0200, monarch_dodra <monarchdodra at gmail.com>
wrote:
> On Thursday, 26 July 2012 at 17:57:31 UTC, Simen Kjaeraas wrote:
>> On Thu, 26 Jul 2012 19:18:21 +0200, monarch_dodra
>> <monarchdodra at gmail.com> wrote:
>>> 2) Is there a "correct" workaround?
>>
>> Exactly what you did. Though, for brevity, you would write this:
>>
>> void seed(T : UIntType)(T value = default_seed)
>
> Thanks
>
> I haven't seen this construct before. Can you tell me a bit more
> about it, or link me to some documentation about it?
>
> I suppose it means "T must be UIntType", but I'd enjoy having a broader
> understanding of it :)
Ali gave the general, I'll give the specifics.
is(T : Foo), void bar(T : Foo)(T t), and a few others (not really others,
they're exactly the same!) means 'T is implicitly convertible to Foo'.
What's the difference, you ask?
Consider:
void foo(T)(T value) if (is(T == uint)) {}
Could you call this function like this:
foo(3);
The answer is no. The compiler translates this to:
foo!(typeof(3))(3);
And typeof(3) is not uint, it's int.
In contrast,
void foo(T : uint)(T value) {}
foo(3);
is also translated to
foo!(typeof(3))(3);
and the compiler then checks if int is implicitly convertible to uint.
And so it is, so the compiler moves happily onwards.
There is a reason I included is(T : Foo) in the beginning, for you
can write the exact same constraint like this:
void foo(T)(T value) if (is(T : uint)) {}
and it will compile just the same.
For more information on this construct, I would, in addition to the
links Ali provided, recommend you read this:
http://dlang.org/expression.html#IsExpression
IsExpressions are, however, probably the most hairy part of D, and
their understanding has proven troublesome to many (myself included,
though I believe I have grasped them now). Hence, most of their
functionality is wrapped in more easily understandable templates in
std.traits.
--
Simen
More information about the Digitalmars-d-learn
mailing list