Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 15, 2013 Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
"Simple things should be simple, complex things should be possible." -Alan Kay I'd like to simplify the syntax of function pre- and post-conditions when the contract block consists of a single assert statement. A special syntax for this special case would omit all of the following: 1) the block's curly braces 2) the assert keyword 3) the semi-colon ending the assert statement 4) the body keyword (if and only if it follows right after the block) So, instead of writing this: int func(int i) in { assert(i < 5); } out(r) { assert(r < 9); } body { return i * 2; } ...you'd be able to write this: int func(int i) in (i < 5) out(r) (r < 9) { return i * 2; } |
June 16, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT Attachments:
| Super awesome idea! How about coma separated expressions to perform multiple asserts?
int func(int i, int j) in(i<5, j<10)
{
return i + j;
}
On 16 June 2013 07:45, TommiT <tommitissari@hotmail.com> wrote:
> "Simple things should be simple, complex things should be possible." -Alan Kay
>
> I'd like to simplify the syntax of function pre- and post-conditions when
> the contract block consists of a single assert statement. A special syntax
> for this special case would omit all of the following:
> 1) the block's curly braces
> 2) the assert keyword
> 3) the semi-colon ending the assert statement
> 4) the body keyword (if and only if it follows right after the block)
>
> So, instead of writing this:
>
> int func(int i)
> in
> {
> assert(i < 5);
> }
> out(r)
> {
> assert(r < 9);
> }
> body
> {
> return i * 2;
> }
>
> ...you'd be able to write this:
>
> int func(int i)
> in (i < 5)
> out(r) (r < 9)
> {
> return i * 2;
> }
>
|
June 16, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 06/16/2013 02:19 AM, Manu wrote:
> Super awesome idea! How about coma separated expressions to perform
> multiple asserts?
>
> int func(int i, int j) in(i<5, j<10)
> {
> return i + j;
> }
>...
Use &&.
|
June 16, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Sunday, 16 June 2013 at 00:19:37 UTC, Manu wrote:
> Super awesome idea! How about coma separated expressions to perform
> multiple asserts?
>
> int func(int i, int j) in(i<5, j<10)
> {
> return i + j;
> }
Do you mean ...to get more specific error messages than with in(i<5 && j<10) ?
|
June 16, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | Am Sun, 16 Jun 2013 04:22:59 +0200 schrieb "TommiT" <tommitissari@hotmail.com>: > On Sunday, 16 June 2013 at 00:19:37 UTC, Manu wrote: > > Super awesome idea! How about coma separated expressions to > > perform > > multiple asserts? > > > > int func(int i, int j) in(i<5, j<10) > > { > > return i + j; > > } > > Do you mean ...to get more specific error messages than with in(i<5 && j<10) ? Actually these are 2 great ideas. Make that expand to: in { assert(i<5, __FUNCTION__ ~ ": precondition i<5 failed. Variables: i == " ~ to!string(i)); assert(j<10, __FUNCTION__ ~ ": precondition j<10 failed. Variables: j == " ~ to!string(j)); } -- Marco |
June 18, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | I posted this to bugzilla as an enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=10396 Perhaps you could vote for it there if you like it. |
June 18, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT Attachments:
| On 16 June 2013 12:22, TommiT <tommitissari@hotmail.com> wrote:
> On Sunday, 16 June 2013 at 00:19:37 UTC, Manu wrote:
>
>> Super awesome idea! How about coma separated expressions to perform multiple asserts?
>>
>> int func(int i, int j) in(i<5, j<10)
>> {
>> return i + j;
>> }
>>
>
> Do you mean ...to get more specific error messages than with in(i<5 &&
> j<10) ?
>
Error messages would be more useful, and it may be much easier for the compiler/optimiser to use this information in the future as separated out into distinct expressions.
|
June 18, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Sunday, 16 June 2013 at 00:19:37 UTC, Manu wrote:
> Super awesome idea! How about coma separated expressions to perform
> multiple asserts?
>
> int func(int i, int j) in(i<5, j<10)
> {
> return i + j;
> }
I find use of comma inside of parentheses of a statement a bit unusual. Correct me if I'm wrong but I don't think there is a single statement in D that separates it's "parts" with a comma. It's always a semi-colon.
So I think it should be:
int func(int i, int j) in (i < 5; j < 10)
{
return i + j;
}
But either comma or a semi-colon used as a separator, this is a really nice syntactic sugar!
|
June 18, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | On Saturday, 15 June 2013 at 21:45:16 UTC, TommiT wrote:
> "Simple things should be simple, complex things should be possible." -Alan Kay
>
> I'd like to simplify the syntax of function pre- and post-conditions when the contract block consists of a single assert statement. A special syntax for this special case would omit all of the following:
> 1) the block's curly braces
> 2) the assert keyword
> 3) the semi-colon ending the assert statement
> 4) the body keyword (if and only if it follows right after the block)
>
> So, instead of writing this:
>
> int func(int i)
> in
> {
> assert(i < 5);
> }
> out(r)
> {
> assert(r < 9);
> }
> body
> {
> return i * 2;
> }
>
> ...you'd be able to write this:
>
> int func(int i)
> in (i < 5)
> out(r) (r < 9)
> {
> return i * 2;
> }
I'd rather reserve this kind of syntax for static contract checking or something similar.
|
June 18, 2013 Re: Feature request: Optional, simplified syntax for simple contracts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aleksandar Ruzicic Attachments:
| What about the argument list only 3 characters earlier?
On 18 June 2013 15:16, Aleksandar Ruzicic <aleksandar@ruzicic.info> wrote:
> On Sunday, 16 June 2013 at 00:19:37 UTC, Manu wrote:
>
>> Super awesome idea! How about coma separated expressions to perform multiple asserts?
>>
>> int func(int i, int j) in(i<5, j<10)
>> {
>> return i + j;
>> }
>>
>
> I find use of comma inside of parentheses of a statement a bit unusual. Correct me if I'm wrong but I don't think there is a single statement in D that separates it's "parts" with a comma. It's always a semi-colon.
>
> So I think it should be:
>
> int func(int i, int j) in (i < 5; j < 10)
> {
> return i + j;
> }
>
>
> But either comma or a semi-colon used as a separator, this is a really nice syntactic sugar!
>
|
Copyright © 1999-2021 by the D Language Foundation