Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Why does use statement choose different method?

John Ky

2010-08-27


Author LoginPost Reply
Hi Groovy List,

In Groovy 1.7.3 I am finding that in the following code, the use statement gives priority to different classes depending on whether the method or property syntax is used:

public class Thing {
}

public class ThingExtra1 {
public static void setValue(
final Thing thing,
final int value) {
println "Set to ThingExtra1"
}

public static String getValue(
final Thing thing) {
return "Get from ThingExtra1"
}
}

public class ThingExtra2 {
public static String setValue(
final Thing thing,
final int value) {
println "Set to ThingExtra2"
}

public static String getValue(
final Thing thing) {
return "Get from ThingExtra2"
}
}

public class GroovyTest {
public static void main(
final String[] args) {
use ([ThingExtra1, ThingExtra2]) {
final Thing thing = new Thing()
thing.value = 1
thing.setValue(1)
println(thing.value)
println(thing.getValue())
}
}
}

The code prints out:

Set to ThingExtra1
Set to ThingExtra2
Get from ThingExtra1
Get from ThingExtra2

I had been expecting one of the following:

Set to ThingExtra1
Set to ThingExtra1
Get from ThingExtra1
Get from ThingExtra1

Or:

Set to ThingExtra2
Set to ThingExtra2
Get from ThingExtra2
Get from ThingExtra2

Why should the use statement prioritise the front of the list in some cases and the end of the list in others?

Cheers,

-John

©2008 gg3721.com - Jax Systems, LLC, U.S.A.