Discussion:
If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.
Daniel.Sun
2007-01-31 14:31:13 UTC
Permalink
Hi all,

When I inspect Groovy, I found a issue that instance(whose class does not
implement plus method) + String will go wrong. For example:

class MyClass {
String toString() {
return "MyClass"
}
}
def mc = new MyClass()
println mc + " is alive"

If you execute above code, you will get following exception:

Exception thrown: groovy.lang.MissingMethodException: No signature of
method: MyClass.plus() is applicable for argument types: (java.lang.String)
values: {" is alive"}

groovy.lang.MissingMethodException: No signature of method: MyClass.plus()
is applicable for argument types: (java.lang.String) values: {" is alive"}
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:572)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at gjdk.groovy.lang.MetaClassImpl_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:119)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at MyClass.invokeMethod(Script1)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:136)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at Script1.run(Script1:7)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:484)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:425)
at gjdk.groovy.lang.GroovyShell_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:131)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:503)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at groovy.lang.Closure.call(Closure.java:188)
at groovy.lang.Closure.call(Closure.java:183)
at groovy.lang.Closure.run(Closure.java:264)
at java.lang.Thread.run(Thread.java:619)


I think the following default implementation of plus method is much better

class MyClass {
String toString() {
return "MyClass"
}
// better default implementation
def plus(String s) {
return toString() + s
}
}

def mc = new MyClass()
println mc + " is alive"

If you execute the above code, you will get your expected result in Java:
MyClass is alive

Best regards,
Daniel.Sun
--
View this message in context: http://www.nabble.com/If-the-default-implementation-of-plus%28%29-is-%22toString%28%29-%2B-s%22%2C-groovy-will-be-more-harmony-with-Java.-tf3149011.html#a8729836
Sent from the groovy - jsr mailing list archive at Nabble.com.


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

http://xircles.codehaus.org/manage_email
John Wilson
2007-01-31 17:32:12 UTC
Permalink
Post by Daniel.Sun
Hi all,
When I inspect Groovy, I found a issue that instance(whose class does not
class MyClass {
String toString() {
return "MyClass"
}
}
def mc = new MyClass()
println mc + " is alive"
the Groovy way to do this is

println "$mc is alive"

We generally discourage String catenation using "+" in Groovy.


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



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

http://xircles.codehaus.org/manage_email
Graeme Rocher
2007-01-31 18:35:41 UTC
Permalink
Post by John Wilson
Post by Daniel.Sun
Hi all,
When I inspect Groovy, I found a issue that instance(whose class does not
class MyClass {
String toString() {
return "MyClass"
}
}
def mc = new MyClass()
println mc + " is alive"
the Groovy way to do this is
println "$mc is alive"
We generally discourage String catenation using "+" in Groovy.
Still regardless of the Groovy way are there any downsides to his
proposal? We should after all try to promote an element of least
surprise for our users

Cheers
Post by John Wilson
John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Graeme Rocher
Grails Project Lead
http://grails.org

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

http://xircles.codehaus.org/manage_email
John Wilson
2007-01-31 19:01:57 UTC
Permalink
Post by Graeme Rocher
Post by John Wilson
We generally discourage String catenation using "+" in Groovy.
Still regardless of the Groovy way are there any downsides to his
proposal? We should after all try to promote an element of least
surprise for our users
I'm not sure - putting metos on Object has to be done with some care
- especially if they are operator methods.

Java doesn't support this, of course.

I think it gets classified as "possibly nice" not "compelling"



John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



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

http://xircles.codehaus.org/manage_email
Martin C. Martin
2007-01-31 19:32:08 UTC
Permalink
I think Obj+String worked two years ago, but not last month. So, I
think we had it in there at one point.

- Martin
Post by Graeme Rocher
Post by John Wilson
We generally discourage String catenation using "+" in Groovy.
Still regardless of the Groovy way are there any downsides to his
proposal? We should after all try to promote an element of least
surprise for our users
I'm not sure - putting metos on Object has to be done with some care -
especially if they are operator methods.
Java doesn't support this, of course.
I think it gets classified as "possibly nice" not "compelling"
John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
John Wilson
2007-01-31 19:47:36 UTC
Permalink
Post by Martin C. Martin
I think Obj+String worked two years ago, but not last month. So, I
think we had it in there at one point.
Two years ago Groovy did lots of odd things :)

Post 1.0 we are trying to be more rigourous in evaluating suggestions.

Two years ago we would have said "cool - lets do it" Now we are
trying to raise the barrier to entry to new features.


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



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

http://xircles.codehaus.org/manage_email
Daniel.Sun
2007-02-01 01:34:25 UTC
Permalink
Hi all,

I think if someone from Java wants to try Groovy and encounters some
unexpected errors, he'll perhaps think it as bugs in Groovy.
Groovy should be harmony with Java, shouldn't it?

Best regards,
Daniel.Sun
Post by John Wilson
Post by Martin C. Martin
I think Obj+String worked two years ago, but not last month. So, I
think we had it in there at one point.
Two years ago Groovy did lots of odd things :)
Post 1.0 we are trying to be more rigourous in evaluating suggestions.
Two years ago we would have said "cool - lets do it" Now we are
trying to raise the barrier to entry to new features.
John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
View this message in context: http://www.nabble.com/If-the-default-implementation-of-plus%28%29-is-%22toString%28%29-%2B-s%22%2C-groovy-will-be-more-harmony-with-Java.-tf3149011.html#a8741580
Sent from the groovy - jsr mailing list archive at Nabble.com.


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

http://xircles.codehaus.org/manage_email
Loading...