Map as interface (and def)

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

Map as interface (and def)

Daniel Henrique Alves Lima
        Hi, everybody.

        Probably a newbie question: Why do they behave differently?


== a.groovy ==
impl = [
  i: 10,
  hasNext: {impl.i > 0},
  next: {impl.i--}
]
iter = impl as Iterator
while (iter.hasNext())
println iter.next()



== b.groovy ==
def impl = [
  i: 10,
  hasNext: {impl.i > 0},
  next: {impl.i--}
]
iter = impl as Iterator
while (iter.hasNext())
println iter.next()



C:\temp>groovy a.groovy
10
9
8
7
6
5
4
3
2
1


C:\temp>groovy b.groovy
Caught: groovy.lang.MissingPropertyException: No such property: impl for class:
b
        at b$_run_closure1.doCall(b.groovy:3)
        at b$_run_closure1.doCall(b.groovy)
        at b.run(b.groovy:7)


thanks.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Alex Tkachman
Because in 1st script you deal with script bound variable and in the
second with local variable

On Thu, Jun 2, 2011 at 3:02 PM, Daniel Henrique Alves Lima
<[hidden email]> wrote:

>        Hi, everybody.
>
>        Probably a newbie question: Why do they behave differently?
>
>
> == a.groovy ==
> impl = [
>  i: 10,
>  hasNext: {impl.i > 0},
>  next: {impl.i--}
> ]
> iter = impl as Iterator
> while (iter.hasNext())
> println iter.next()
>
>
>
> == b.groovy ==
> def impl = [
>  i: 10,
>  hasNext: {impl.i > 0},
>  next: {impl.i--}
> ]
> iter = impl as Iterator
> while (iter.hasNext())
> println iter.next()
>
>
>
> C:\temp>groovy a.groovy
> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
>
>
> C:\temp>groovy b.groovy
> Caught: groovy.lang.MissingPropertyException: No such property: impl for class:
> b
>        at b$_run_closure1.doCall(b.groovy:3)
>        at b$_run_closure1.doCall(b.groovy)
>        at b.run(b.groovy:7)
>
>
> thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Jochen Theodorou
Am 02.06.2011 17:09, schrieb Alex Tkachman:
> Because in 1st script you deal with script bound variable and in the
> second with local variable

to correct the script you have to write:

def impl
impl = [
   i: 10,
   hasNext: {impl.i>  0},
   next: {impl.i--}
]
iter = impl as Iterator
while (iter.hasNext())
println iter.next()

his is because the variable impl is only known after the definition, but
you use it while you define it.

bye blackdrag
--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead
http://blackdragsview.blogspot.com/
For Groovy programming sources visit http://groovy.codehaus.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Daniel Henrique Alves Lima
In reply to this post by Daniel Henrique Alves Lima
@Jochen: Yes, that was my way of "fixing" it. Thanks.

@Alex: Thanks. Now I'm trying to figure out why (or how) the first
script "works" without error: Is it because the script bound variables
are lazy evaluated and the local variables aren't?

I don't know how to properly ask this question :-)


http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22


Thanks!

PS: And to make life easier (or harder) for the next newbie asking the
same question:

def impl = [
    i: 10,
    hasNext: {impl2.i > 0},
    next: {impl2.i--}
]
impl2 = impl
iter = impl as Iterator
while (iter.hasNext())
println iter.next()

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Jochen Theodorou
Am 02.06.2011 19:24, schrieb Daniel Henrique Alves Lima:
> @Jochen: Yes, that was my way of "fixing" it. Thanks.
>
> @Alex: Thanks. Now I'm trying to figure out why (or how) the first
> script "works" without error: Is it because the script bound variables
> are lazy evaluated and the local variables aren't?

if you have this:

> impl = [
>      i: 10,
>      hasNext: {impl.i>  0},
>      next: {impl.i--}
> ]

it is seen more like this:

 > this.binding.impl = [
 >      i: 10,
 >      hasNext: {this.binding.impl.i>  0},
 >      next: {this.binding.impl.i--}
 > ]

it is comparable to a field a bit

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead
http://blackdragsview.blogspot.com/
For Groovy programming sources visit http://groovy.codehaus.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Daniel Henrique Alves Lima
In reply to this post by Daniel Henrique Alves Lima
        Ok.

        Borrowing the "declaration vs. definition" terminology from C,
may I say that when a name is not resolved to a local variable
declaration, then the Groovy analyzer/compiler/interpreter assumes
that the variable will be "dynamic" declared?

        When doing

impl = [
    i: 10,
    hasNext: {impl2.i > 0},
    next: {impl2.i--}
]

        I'm *defining* impl and saying that impl2 will be *declared*
"later", so Groovy doesn't need to worry right now.

        The same holds true for:

impl = [
    i: 10,
    hasNext: {impl.i > 0},
    next: {impl.i--}
]


        The latter syntax has the advantage of being more concise and
elegant, but a little confusing for beginners :-)

        Thanks.


http://groovy.codehaus.org/Groovy+way+to+implement+interfaces

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map as interface (and def)

Jochen Theodorou
Am 02.06.2011 20:17, schrieb Daniel Henrique Alves Lima:
>          Ok.
>
>          Borrowing the "declaration vs. definition" terminology from C,
> may I say that when a name is not resolved to a local variable
> declaration, then the Groovy analyzer/compiler/interpreter assumes
> that the variable will be "dynamic" declared?

yes, absolutely right

bye blackdrag


--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead
http://blackdragsview.blogspot.com/
For Groovy programming sources visit http://groovy.codehaus.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email