New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpolation in assertion failure message #3692

Closed
CeylonMigrationBot opened this Issue Feb 6, 2013 · 21 comments

Comments

Projects
None yet
8 participants
@CeylonMigrationBot

CeylonMigrationBot commented Feb 6, 2013

[@gavinking] When we implemented assertions, we left one issue for later: how to interpolate expressions into the assertion failure message. Right now, we can write:

"exponent must be non-negative"
assert (power>=0);

Where the string literal is an abbreviated doc annotation. I've defended this solution on the grounds that it lets ceylondoc include the preconditions on parameters in its generated documentation. However, in truth, we're eventually going to need interpolation here. So the question is: what should the syntax be. Clearly, the following is one possibility:

"exponent ``power`` must be non-negative"
assert (power>=0);

Could ceylondoc do something reasonable with this? I don't see why not. But is it just to "weird" to suddenly let you have interpolation in a doc string, all of a sudden, in one special case?

Alternatively, we could let you write something like this:

assert (power>=0) "exponent ``power`` must be non-negative";

or this:

assert (power>=0) => "exponent ``power`` must be non-negative";

The => is not strictly necessary. But for some reason I find it more natural to format across multiple lines. This:

assert (power>=0) 
        "exponent ``power`` must be non-negative";

is to me less natural than this:

assert (power>=0) 
        => "exponent ``power`` must be non-negative";

Whatever, either option would be acceptable to me.

WDYT?

[Migrated from ceylon/ceylon-spec#586]

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Jul 8, 2013

[@gavinking] I've so far had zero (0.0) feedback on this issue. Thoughts?

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Jul 8, 2013

[@tombentley] I do like

assert (power>=0) => "exponent ``power`` must be non-negative";

assuming ceylondoc is still allowed to document this when it occurs as a precondition.

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Jul 8, 2013

[@gavinking]

assuming ceylondoc is still allowed to document this when it occurs as a precondition.

Well, that's the real question: what is ceylondoc going to do with this shit? I suppose it can render a link to the parameter, no?

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Jul 8, 2013

[@DiegoCoronel] We had a similar discussion in #3725 , i did not know about this issue.

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Jul 9, 2013

[@gavinking] Well, I kinda implemented this in branch 586, but now I'm starting to doubt the usefulness of it. I think it's better to at least slip this feature from M6.

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Aug 29, 2013

[@gavinking] Not for 1.0.

@CeylonMigrationBot

This comment has been minimized.

CeylonMigrationBot commented Nov 26, 2013

[@ncorai] Now that all validation is done through assert statements, it would be really nice to have interpolation in the message for milestone 1.1. The longer we wait the more the existing code will be littered with not-quite-useful error messages.

@ncorai

This comment has been minimized.

ncorai commented Sep 24, 2016

I regret to say that release 1.5 feels as far away today as it did 3 years ago. How about 1.3.1 instead? Pretty please 😃

@tombentley

This comment has been minimized.

Contributor

tombentley commented Sep 24, 2016

Yes it doesn't seem so hard to do and would be really useful.

@bjansen

This comment has been minimized.

Contributor

bjansen commented Feb 15, 2017

Today I converted Java code to Ceylon and needed an interpolated assert message, but unfortunately I had to write that message as a comment instead.

FTR I like the fat arrow used in the initial comment, it kinda looks like what I would write when taking notes on paper:

something something => explanation
@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 17, 2017

@bjansen

but unfortunately I had to write that message as a comment instead.

Huh? What's wrong with if (...) { throw AssertionError(....); }?

@bjansen

This comment has been minimized.

Contributor

bjansen commented Feb 17, 2017

I hadn't thought of this :)

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 20, 2017

😀

@gavinking gavinking modified the milestones: 1.3.2, 1.5, 1.3.3 Feb 23, 2017

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 23, 2017

I have implemented support for this in the typechecker. However, the backends need to be modified to support this (which I assume is a pretty trivial change).

@tombentley? @chochos?

gavinking added a commit that referenced this issue Feb 23, 2017

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 23, 2017

Now available on the 3692 branch.

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 24, 2017

I've implemented support in the JVM backend. JS still to go.

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 24, 2017

@lucaswerkmeister note that this change will affect ceylon.ast

@gavinking

This comment has been minimized.

Contributor

gavinking commented Feb 24, 2017

Done!

@gavinking gavinking closed this Feb 24, 2017

lucaswerkmeister added a commit to eclipse/ceylon.formatter that referenced this issue Feb 24, 2017

Add test for interpolated assertion messages
See eclipse/ceylon#3692. Apparently they work without any changes to
formatter code, yay!
@xkr47

This comment has been minimized.

Contributor

xkr47 commented Feb 24, 2017

Seems this was the chosen syntax?

"exponent ``power`` must be non-negative"
assert (power>=0);
@lucaswerkmeister

This comment has been minimized.

Member

lucaswerkmeister commented Feb 24, 2017

Thanks for the heads-up @gavinking! (Formatter works without changes, nice!)

gavinking added a commit that referenced this issue Feb 25, 2017

@chochos

This comment has been minimized.

Contributor

chochos commented Feb 28, 2017

Looks really cool. Reminds me of groovy's "power assert". Just updated the assertion messages in the check module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment