digitalmars.D.learn - Pre and Post contracts and signature constraints
- Zardoz <luis.panadero gmail.com> Jul 04 2011
- simendsjo <simendsjo gmail.com> Jul 04 2011
- simendsjo <simendsjo gmail.com> Jul 04 2011
- simendsjo <simendsjo gmail.com> Jul 04 2011
- Zardoz <luis.panadero gmail.com> Jul 04 2011
How is the correct way of mix Pre and Post contracts and signature constraints
?
I try this that looks logic way and not compile :
T foo (T) (T a)
if ( is(T : real)) {
in {
assert (a > 0);
}
body {
return a*2;
}
}
I get this errors :
Declaration expected, not 'if'
unrecognized declaration
And this with same result :
T foo (T) (T a)
in {
assert (a > 0);
}
if ( is(T : real)) {
body {
return a*2;
}
}
I'm using dmd 2.053.
--
Yep, I'm afraid that I have a blog : zardoz.es
Jul 04 2011
On 04.07.2011 19:20, Zardoz wrote:How is the correct way of mix Pre and Post contracts and signature constraints ? I try this that looks logic way and not compile : T foo (T) (T a) if ( is(T : real)) { in { assert (a> 0); } body { return a*2; } } I get this errors : Declaration expected, not 'if' unrecognized declaration And this with same result : T foo (T) (T a) in { assert (a> 0); } if ( is(T : real)) { body { return a*2; } } I'm using dmd 2.053.
The "in" should be in a block of it's own: T foo(T)(T a) if(...) // notice no { here in { } body { }
Jul 04 2011
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.
Jul 04 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.
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);
Jul 04 2011
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.
I thought that the if () needs to make a block. :P -- Yep, I'm afraid that I have a blog : zardoz.es
Jul 04 2011









simendsjo <simendsjo gmail.com> 