#1789 Regression: verifyErr throws "Not a statement" in 1.0.62

tonsky Sat 25 Feb 2012

Hi, after upgrading to 1.0.62 I got following error when using sys::Test.verifyErr:

class MyTest : Test {
  Void testSmth() {
    verifyErr(ParseErr#) { Int.fromStr("@#!") }
  }
}


[~/Dropbox/ws/xored/spectre/src/test] fan MyTest.fan 
/Users/prokopov/Dropbox/ws/xored/spectre/src/test/MyTest.fan(3,32): Not a statement
ERROR: cannot compile script

It was working in 1.0.61, and code I used here was copied directly from http://fantom.org/doc/sys/Test.html#verifyErr

Thanks!

brian Sat 25 Feb 2012

That is related to the static new constructor change. Now they are true constructors, and the compiler doesn't allow a constructor call with it being assigned to something or used as a parameter. What I do is just assign to throw away local variable:

x := Int.fromStr("@#!")

tonsky Tue 28 Feb 2012

Thanks for the answer, Brian.

Why cannot be constructors used as a return value?

And, I think, you should also update example in the docs to be compileable?

SlimerDude Tue 28 Feb 2012

Why cannot be constructors used as a return value?

Not sure what you mean, they are! They return the object you've just constructed! It's just that you currently have to do something with that object - and not leave it hanging.

What I do is just assign to throw away local variable: x := Int.fromStr("@#!")

Hmm, when F4 gets updated (fingers crossed real tight!) it may introduce warnings for unused variables, like the one above. Maybe call some innocuous method instead, like

Int.fromStr("@#!").toStr

Other than it's bad programming to have contructors do work, what's the reason for the Fantom compiler to out right disallow it, and force assignment or usage?

And, I think, you should also update example in the docs to be compileable?

I agree, the docs are the Fantom Bible and to help out new comers (like me!), they should always be correct, complete and upto date.

brian Tue 28 Feb 2012

I have already changed the documentation.

What Fantom disallows is using a constructor as a complete statement. For example you can't use a literal 71 as a statement, nor can you use Date("2010-01-02") either. That expression has to be assigned to a variable, used as a parameter, or returned as result of a function or method.

As a general policy just constructing an object and not doing anything with it should be a side-effect free operation that is indeed a bug. This is especially tricky in certain places in Fantom where this compiler error is very useful. Consider:

// this works
GridPane { Label { text="hi" }, }

// this is broken and will report a compiler error because 
// you aren't actually using the Label instance
GridPane { Label { text="hi" } }

When we are testing that a function throws an exception, we are actually interested in the side effect, but we have to do something with the result to please the compiler.

SlimerDude Tue 28 Feb 2012

Fair enough - but don't you mean:

// this works
x: = GridPane { Label { text="hi" }, }

:)

Login or Signup to reply.