when to fail while resolving names to class members at compile time

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

when to fail while resolving names to class members at compile time

Jochen Theodorou
Hi,

I have the following script

      class ForLoopTest {
        def testme() {
          for (i in [1,2]) {
          }
          println i
        }
      }
   
      def f = new ForLoopTest()
      f.testme()

as you may see, the "println i" may reference to the variable i used in
the for-loop. As this variable is only visible in the loop and not
outside the println i will fail.

My question is if this shouldn't fail at compile time. I can enable this
by adding 3 lines. I remember we talked about dynamic resolving and
static resolving classes a little in Paris, but now I should have a
decision. Has the class to be marked somehow to indicate that the "i" of
the println statement should cause the compiler to handle the i as
property and with this let the runtime resolve it dynamically or should
this in general be resolved dynamically?

NOTE: my current scoping implementation expects dynamic and static
scopes where a dynamic scope is resolved at runtime. Normally classes
are a static scope and closures and scripts are dyanamic scopes.

bye blackdrag


Reply | Threaded
Open this post in threaded view
|

Re: when to fail while resolving names to class members at compile time

Guillaume Laforge
Administrator
On 20/01/06, Jochen Theodorou <[hidden email]> wrote:

> I have the following script
>
>            class ForLoopTest {
>              def testme() {
>                for (i in [1,2]) {
>                }
>                println i
>              }
>            }
>
>            def f = new ForLoopTest()
>            f.testme()


"i" should be resolved at runtime against the metaclass of the ForLoopTest.
So the default metaclass will certainly throw an exception at runtime
saying there's a property missing.

But it might be interesting to have an optional flag in the compiler
or a marker annotation to say that the class should be considered more
strictly so that at least a warning be returned in case such non-bound
names are found: @WarnUnboundName or something...

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy