If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

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

If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

Daniel Sun
Hi all,

When I inspect Groovy, I found a issue that instance(whose class does not override 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
Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

tugwilson

On 31 Jan 2007, at 14:31, Daniel.Sun wrote:

> 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"


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

Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

graemer
On 1/31/07, John Wilson <[hidden email]> wrote:

>
> On 31 Jan 2007, at 14:31, Daniel.Sun wrote:
>
> > 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"
>
>
> 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

>
>
> 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
Grails Project Lead
http://grails.org

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

tugwilson

On 31 Jan 2007, at 18:35, Graeme Rocher wrote:

>> 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

Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

Martin C. Martin-2
I think Obj+String worked two years ago, but not last month.  So, I
think we had it in there at one point.

- Martin

John Wilson wrote:

>
> On 31 Jan 2007, at 18:35, Graeme Rocher wrote:
>
>>> 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
>

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

tugwilson

On 31 Jan 2007, at 19:32, Martin C. Martin wrote:

> 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

Reply | Threaded
Open this post in threaded view
|

Re: If the default implementation of plus() is "toString() + s", groovy will be more harmony with Java.

Daniel Sun
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
John Wilson wrote
On 31 Jan 2007, at 19:32, Martin C. Martin wrote:

> 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