digitalmars.D.learn - Parameter specialization
- Eyyub (25/25) Jul 20 2012 Hello,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/37) Jul 20 2012 The confusion is due to the two different meanings of the ':' operator.
- Artur Skawina (7/42) Jul 21 2012 No; it would be considered. The problem is likely related to "Function t...
- Eyyub (4/4) Jul 21 2012 Okay !
Hello,
I have a question about the semantic of parameter
specialization(TemplateTypeParameterSpecialization)
In this following code, there are 2 forms of the same function
'add' :
<code>
T add(T, U : T) (T a, U b) //doesn't work
{
return a + b;
}
T add(T, U) (T a, U b) if(is(U : T)) //works
{
return a + b;
}
void main()
{
assert(add(2, cast(short)2) == 4);
}
</code>
So, I infer that, in this case,
TemplateTypeParameterSpecialization and TypeSpecialization(of
IsExpression) aren't semantically equal ? What are differences
between this 2 forms ?
Eyyub,
(I hope that I have an english a bit compréhensible)
Jul 20 2012
On 07/20/2012 06:47 PM, Eyyub wrote:
Hello,
I have a question about the semantic of parameter
specialization(TemplateTypeParameterSpecialization)
In this following code, there are 2 forms of the same function 'add' :
<code>
T add(T, U : T) (T a, U b) //doesn't work
{
return a + b;
}
T add(T, U) (T a, U b) if(is(U : T)) //works
{
return a + b;
}
void main()
{
assert(add(2, cast(short)2) == 4);
}
</code>
So, I infer that, in this case, TemplateTypeParameterSpecialization and
TypeSpecialization(of IsExpression) aren't semantically equal ? What are
differences between this 2 forms ?
Eyyub,
(I hope that I have an english a bit compréhensible)
The confusion is due to the two different meanings of the ':' operator.
1) In the template parameter list, ':' specializes U. "U : T" means:
"this is a specialization where U is T".
http://dlang.org/template.html
(Search for the "Specialization" section.)
When you use the template, T is int and U is short; so that
specialization is not considered.
2) In an is expression, ':' means "implicitly convertible to".
http://dlang.org/expression.html#IsExpression
So "is(U : T)" matches because "short is implicitly convertible to int."
Ali
--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jul 20 2012
On 07/21/12 08:29, Ali Çehreli wrote:On 07/20/2012 06:47 PM, Eyyub wrote:No; it would be considered. The problem is likely related to "Function template type parameters that are to be implicitly deduced may not have specializations". Note that T add(T, U : int) (T a, U b) {...} already works, and "add(T, U : T)" could probably be made to work too. arturI have a question about the semantic of parameter specialization(TemplateTypeParameterSpecialization) In this following code, there are 2 forms of the same function 'add' : <code> T add(T, U : T) (T a, U b) //doesn't work { return a + b; } T add(T, U) (T a, U b) if(is(U : T)) //works { return a + b; } void main() { assert(add(2, cast(short)2) == 4); } </code> So, I infer that, in this case, TemplateTypeParameterSpecialization and TypeSpecialization(of IsExpression) aren't semantically equal ? What are differences between this 2 forms ?The confusion is due to the two different meanings of the ':' operator. 1) In the template parameter list, ':' specializes U. "U : T" means: "this is a specialization where U is T". http://dlang.org/template.html (Search for the "Specialization" section.) When you use the template, T is int and U is short; so that specialization is not considered.
Jul 21 2012
Okay ! The spec' should be more explicit, I'm sure that I'm not the first who asked this question ! Thanks Guys,
Jul 21 2012








"Eyyub" <eyyub.pangearaion gmail.com>