www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Parameter specialization

reply "Eyyub" <eyyub.pangearaion gmail.com> writes:
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
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
prev sibling next sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 07/21/12 08:29, Ali Çehreli wrote:
 On 07/20/2012 06:47 PM, Eyyub wrote:
 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 ?

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.

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. artur
Jul 21 2012
prev sibling parent "Eyyub" <eyyub.pangearaion gmail.com> writes:
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