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
<http://docs.groovy-lang.org/latest/html/documentation/type-checking-extensions.html>
.



2016-02-04 10:10 GMT+01:00 Anton Sarov <izvanz3mn...@yahoo.com>:

> 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 regards
> Anton
>
>
> On Wednesday, February 3, 2016 10:43 PM, Pascal Schumacher <
> pascalschumac...@gmx.net> 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>
> 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
>
>
>
>
>

Reply via email to