You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Anton Sarov <iz...@yahoo.com> on 2016/02/03 16:24:21 UTC

GroovyShell Binding with TypeChecked

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 regardsAnton
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 errorstartup 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 errorstartup failed:
Script1.groovy: 7: [Static type checking] - The variable [util] is undeclared.
 @ line 7, column 17.
           def a = util.test();
                   ^

1 error

Re: GroovyShell Binding with TypeChecked

Posted by Cédric Champeau <ce...@gmail.com>.
There *is* such an example: take a look at how the markup template engine
is implemented. It also uses a binding, and provides type information
through type checking extensions. It's not the simplest example ever,
though.

2016-02-04 14:29 GMT+01:00 Jochen Theodorou <bl...@gmx.org>:

>
>
> On 04.02.2016 11:18, Anton Sarov wrote:
> [...]
>
>> 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".
>>
>
> the compiler has no idea about what is in the binding. In other words,
> that link is missing. As of how to provide that link... no idea actually.
> The architecture isolates the compiler quite a lot from all kinds of
> runtime data like a binding normally. Even with extensions, you normally do
> not target actual runtime data, but an abstraction of that provided in a
> way known to the extension.
>
> bye Jochen
>

Re: GroovyShell Binding with TypeChecked

Posted by Jochen Theodorou <bl...@gmx.org>.

On 04.02.2016 11:18, Anton Sarov wrote:
[...]
> 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".

the compiler has no idea about what is in the binding. In other words, 
that link is missing. As of how to provide that link... no idea 
actually. The architecture isolates the compiler quite a lot from all 
kinds of runtime data like a binding normally. Even with extensions, you 
normally do not target actual runtime data, but an abstraction of that 
provided in a way known to the extension.

bye Jochen

Re: GroovyShell Binding with TypeChecked

Posted by Anton Sarov <iz...@yahoo.com>.
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 <ce...@gmail.com> 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 <iz...@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 regardsAnton


    On Wednesday, February 3, 2016 10:43 PM, Pascal Schumacher <pa...@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 
  
 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  
 
 

   



  

Re: GroovyShell Binding with TypeChecked

Posted by Cédric Champeau <ce...@gmail.com>.
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 <iz...@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 <
> pascalschumacher@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
>
>
>
>
>

Re: GroovyShell Binding with TypeChecked

Posted by Anton Sarov <iz...@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 regardsAnton


    On Wednesday, February 3, 2016 10:43 PM, Pascal Schumacher <pa...@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 
  
 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  
 
 

   

Re: GroovyShell Binding with TypeChecked

Posted by Pascal Schumacher <pa...@gmx.net>.
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