nhatminhle / cofoja

Contracts for Java
GNU Lesser General Public License v3.0
151 stars 18 forks source link

Implies => operator should be replaced by the conditional operator "?" #14

Closed nhatminhle closed 9 years ago

nhatminhle commented 9 years ago

From ckkohl79 on February 05, 2011 11:29:56

The introduction of a new operator "=>" for conditional checks is annoying at least, and in the worst case dangerous if another "=>" operator would be coming to Java at some point.

Instead you should just simply use the conditional operator "?", and make the "else" part (starting with ":") optional.

Example: @Requires("foo != null => foo.size() > 0")

should be rather written as @Requires("foo != null ? foo.size() > 0")

This is not only more consistent, but also cleaner to write @Requires("foo != null ? foo.size() > 0 : foo.size() == 0")

than @Requires("foo != null => foo.size() > 0") @Requires("foo == null => foo.size() == 0")

Cheers, Christian

Original issue: http://code.google.com/p/cofoja/issues/detail?id=7

nhatminhle commented 9 years ago

From ckkohl79 on February 05, 2011 11:35:46

The last example should of course read

than @Requires("foo != null => foo.size() > 0", "foo == null => foo.size() == 0")

nhatminhle commented 9 years ago

From nhat.min...@gmail.com on February 05, 2011 15:52:11

I reckon this could be a problem if e.g. Java decides to add => for lambdas or something. I don't have any strong opinion on the subject, though, and could as well revert to using something else (e.g. !||).

We do not ship with a full-blown expression parser, though, so not all syntax changes can be done, but I believe ?: without an else part could be hacked in because it has such low precedence (similarly to what we do with => already). I'll investigate more if we decide this should be done.

I've assigned the issue to Andreas, who's mainly responsible for the design of the specification language itself.

Status: Accepted
Owner: andreasl...@google.com

nhatminhle commented 9 years ago

From ckkohl79 on February 06, 2011 02:40:41

Excellent!

Cheers, Christian

nhatminhle commented 9 years ago

From andreasl...@google.com on February 06, 2011 09:09:24

I agree, if a Java extension introducing a => operator would leave us in a bad place and the proposal with the conditional operator doesn't sound bad.

nhatminhle commented 9 years ago

From smassey1...@gmail.com on April 14, 2011 15:52:53

Cofoja: have you considered an Upgrade to javassit as ther is no requirement to write your own high level parser of syntax? See chex4j.

nhatminhle commented 9 years ago

From chat...@google.com on May 16, 2011 02:41:47

Hi folks, after some discussion, we decided to remove the imply operator. This was done on r118 . However, the ":" part of the conditional operator "?" was NOT made optional as suggested. We want to minimize the number additions on contracts as much as we can.

Instead of doing "foo != null => foo.size() > 0" now you can do "foo == null || foo.size() > 0", which has the exact same meaning.

Hi smassey, thanks for your comment and I'm really sorry for the delay on my answer. Currently we don't have any special extra need for our parser, but I'll keep this in mind. Thanks again!

Status: Done