Pre and Post contracts and signature constraints

simendsjo simendsjo at gmail.com
Mon Jul 4 11:37:28 PDT 2011


On 04.07.2011 20:12, Zardoz wrote:
> On Mon, 04 Jul 2011 19:52:10 +0200, simendsjo wrote:
>
>> On 04.07.2011 19:42, simendsjo wrote:
>>>
>>> The "in" should be in a block of it's own:
>>>
>>> T foo(T)(T a)
>>> if(...) // notice no { here
>>> in {
>>> } body {
>>> }
>>
>> I of course mean that if should not create a new block.
> Thanks !
> I thought that the if () needs to make a block. :P

The if is called a template constrain: 
http://www.d-programming-language.org/template.html#Constraint

T foo(T)(T a) is a shorthand (I think) for

template foo(T) {
   T foo(T a) {
   }
}

The constraint refers to the template part:

template foo(T) if(is(T:real)) {
   T foo(T a) {
   }
}

Because the template only contain a single member, and it's named the 
same as the template, you can instantiate it directly as foo(1.2);
If the function was named bar, you'd have to instantiate it yourself: 
foo!real.bar(1.2);


More information about the Digitalmars-d-learn mailing list