Hi Cedric,
thanks for the explanation. This is in fact what I did - I defined a type
checking extension. However I always wondered why my binding is not regarded by
the type cheker.
There is still one point which I do not get, even if I put myself in the place
of the compiler. Imagine I have my script "A" which uses the Util class.
1) I try to compile "A". Unless I have defined a type checker extension this
should fail because the compiler has absolutely no way to know that the "util"
variable is actually from type "Util". So far so good, I understand this. But
now
2) I try to compile "A" and provide some binding to the
GroovyShell/GroovyClassLoader/etc. - I would expect that the binding "takes the
role" of the type checker extension. So that the compiler would say "Oh, look,
there is a variable whose type I do not know, but I have it here in the
binding, so I will use this one".
I still do not get why this is not feasible. And that was my question in the
previous post - is it so, that the compiler ignores the binding. Should I think
of the binding as something only "runtime-like".
Best regardsAnton
On Thursday, February 4, 2016 10:22 AM, Cédric Champeau
<[email protected]> wrote:
Hi Anton,
First of all, your code is invalid even without @TypeChecked. I guess what you
want is to execute a *script* at runtime, that uses a TypeChecked class that is
precompiled. It means that you have 2 conflicting timelines. The first one is
what happens when you compile the class you want to make available to scripts,
and the second is what happens when the script itself is compiled (at runtime).
You have to ask yourself: what does the type checker knows when it compile your
class, vs what *you* know when your code is executed. Be a machine, and look at
your code: when A is compiled, the compiler knows *nothing* about "util". It's
only because you, as a developer, know that "util" is going to be provided by
the binding, that you, as a developer, can infer the type of that variable (and
in practice, since your code is wrong, "util" wouldn't be visible anyway
because it's used in a class scope, not in a script, but let's forget about
that).
To illustrate this, you can totally imagine that your binding is going to be
modified during execution of the script. One could write "util = new Date()",
and the compiler would be totally wrong. That's why, you, as a developer, have
to help the compiler, to tell it that actually *you* know that the type of
"util" is, because you will provide it through the binding, or through a super
script class, or .... That's what type checking extensions are meant for. I'd
recommend that you read the appropriate section in the userguide.
2016-02-04 10:10 GMT+01:00 Anton Sarov <[email protected]>:
Hi Pascal,
I realize that the binding is just a map but I still do not see the
implication. The type checker could very well take the variable name - in my
case "util" and look it up in the map/binding - just to see if there is such a
key.
Now I am having the feeling that the type checker does not use the binding/map
at all. Am I correct?
Best regardsAnton
On Wednesday, February 3, 2016 10:43 PM, Pascal Schumacher
<[email protected]> wrote:
Hi Anton,
Binding is essentially just a map. The typ checker does not know which values
will be present. Therefore it flags "util" as undeclared.
Cheers,
Pascal
Am 03.02.2016 um 16:24 schrieb Anton Sarov:
Hello,
I have the following case where I want to make use of the type checking
feature:
http://groovyconsole.appspot.com/script/5121843795066880
Unfortunately I get an error like: "The variable [util] is undeclared".
Why is this happening? I defined a variable in the provided binding but
apparently this is somehow not relevant to the Groovy Shell...
Best regards Anton
startup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
@ line 7, column 17.
def a = util.test();
^
1 error startup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
@ line 7, column 17.
def a = util.test();
^
1 error startup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
@ line 7, column 17.
def a = util.test();
^
1 errorstartup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
@ line 7, column 17.
def a = util.test();
^
1 error startup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
@ line 7, column 17.
def a = util.test();
^
1 error