Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

assert with body #4540

Open
CeylonMigrationBot opened this issue Oct 5, 2015 · 20 comments
Open

assert with body #4540

CeylonMigrationBot opened this issue Oct 5, 2015 · 20 comments

Comments

@CeylonMigrationBot
Copy link

[@gavinking] In #3997 it was proposed that we could support stuff like this:

assert (exists length, nonempty sequence, length>0) {
    return null;
}

Even though #3997 is now implemented, there may still be value in that. I personally find it to be a more pleasing and more powerful way to write "guards".

[Migrated from ceylon/ceylon-spec#1434]

@CeylonMigrationBot
Copy link
Author

[@lucaswerkmeister] I still think it’s much clearer with else, i. e.

assert (exists length, nonempty sequence, length>0) else {
    return null;
}

@CeylonMigrationBot
Copy link
Author

[@FroMage] So the difference with if is that the new values scope extends to the end like normal assert, and the difference with normal assert is the failure code that can be customised rather than just throw, right?

@CeylonMigrationBot
Copy link
Author

[@gavinking] Well the really big difference is that to write the above with if, I would have to write:

if (!exists length) {
    return null;
}
if (!nonempty sequence) {
    return null;
}
if (length<=0) {
    return null;
}

Which is significantly worse.

@CeylonMigrationBot
Copy link
Author

[@quintesse] I can't help but intensely dislike this, but I don't see any other option since we decided a long time ago to have this , notation in conditions.

@CeylonMigrationBot
Copy link
Author

[@Zambonifofex] Honestly, I agree with @quintesse. I think a better aproach would be to allow people to put ! before conditional thingies:

if!(exists bar, foo())
{
    assert(false);
}
bar.baz();

Same for while!(), and assert!()...

But also, I agree with @gavinking that these "guard" thingles are not very ceylonic. If you end up with a bunch of ifs guarding your actual code, then just put your code actually inside the if block...

if(!exists foo) { return(null); }
if(!exists bar) { return(null); }
if(!exists baz) { return(null); }
// actual code

becomes

if!(exists foo, exists bar, exists baz)
{
    // actual code
}
else
{
    return(null);
}

@CeylonMigrationBot
Copy link
Author

[@bjansen] Oh so now I could rewrite this Java code:

if (foo == null || foo == bar) {
    return;
}

to a nice

assert(exists foo, foo != bar) {
    return;
}

instead of the ugly

if (foo?.equals(bar) else true) {
    return;
}
assert(exists foo);

I like that 👍

@CeylonMigrationBot
Copy link
Author

[@RossTate] I agree with @lucaswerkmeister, the else is necessary to make this clear to people not familiar with the feature.

@CeylonMigrationBot
Copy link
Author

[@luolong] I somewhat dislike the else after the assert condition.

Although, I can see how it makes the it more explicit when the block is being invoked...

I would however add an additional condition to the assertion failure block that it has to definitely return.

Otherwise it is indistinguishable from if

@CeylonMigrationBot
Copy link
Author

[@gavinking]

I would however add an additional condition to the assertion failure block that it has to definitely return.

Yes, of course, I'm assuming that.

@CeylonMigrationBot
Copy link
Author

[@FroMage] I'd also put an else.

@CeylonMigrationBot
Copy link
Author

[@davidfestal] +1 for the else also

@CeylonMigrationBot
Copy link
Author

[@lukedegruchy] +1 for else.

@CeylonMigrationBot
Copy link
Author

[@ncorai] +1 for else

@CeylonMigrationBot
Copy link
Author

[@Zambonifofex] I thought I had posted this already, but apparently not: besides my distaste for this feature (and my preference for if!()), I prefer the version with else as well. Not exclusively because it makes it easier it easier for people unfamiliar with the feature to understand, but also because it makes more sense syntactic-wise. It bring the syntax closer to the if structure, having the else block contain code that is executed when the structure's normal behavior isn't performed...

@CeylonMigrationBot
Copy link
Author

[@jvasileff] I really like the idea, but I'm not sure about the syntax. assert suggests the input is invalid, or at least deficient, making many uses of this powerful feature look like abuses.

The if!() idea doesn't make sense to me, because ! is confusing. Are all of the conditions negated? What happens to the ,s?

If any were a keyword, I'd suggest:

if any (!exists length, !nonempty sequence, length <= zero) {
    return;
}

perhaps subtle, but another idea would be to use ;s for the || analog to &&'s , in if statements:

if (!exists length; !nonempty sequence; length <= zero) {
   return;
}

@CeylonMigrationBot
Copy link
Author

[@Zambonifofex]

The if!() idea doesn't make sense to me, because ! is confusing. Are all of the conditions negated? What happens to the ,s?

The ! means that all the conditions should be false.

if!(foo, bar, baz)
{
    print("hi");
}

is equivalent to

if(!(foo && bar && baz))
{
    print("hi");
}

Of course, that substituition wouldn't work for is, exists and nonempty...

@xkr47
Copy link
Contributor

xkr47 commented Jan 2, 2016

Can't help but think of unless from Perl:

unless(exists a, exists b) {
  return null;
}

The proposed if! syntax would be my second favourite. A third option could even be:

if (exists a, exists b) else {
    return null;
}

.. but I guess that else would be easy to overlook..

Extending assert would indeed look better with else.

I guess assert would also require that the block always terminates the containing function call somehow - otherwise it would not be an assertion? Unless and if! would not have this requirement I guess.

@ghost
Copy link

ghost commented Jan 4, 2016

@xkr47 I suggested if!, because I think it's be neat and regular to allow it with while and assert as well (while!(is Null foo, is String bar), assert!(is Null foo, is String bar). I would also prefer meaningful keywords (unless, until, ???), but introducing new keywords is generally not a good idea...

@jvasileff
Copy link
Contributor

I like the if! syntax with a small whitespace tweak:

if !(exists length, nonempty sequence, length > 0) {
    return null;
}   

@xkr47
Copy link
Contributor

xkr47 commented Jan 4, 2016

@Zambonifofex true..
@jvasileff not far from:

if (!(exists length, nonempty sequence, length > 0)) {

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants