[ 
https://issues.apache.org/jira/browse/GROOVY-6837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17474491#comment-17474491
 ] 

Paul King commented on GROOVY-6837:
-----------------------------------

I think the LUB idea is too fancy in this scenario. Our current behavior is as 
follows:
{code}
import static groovy.test.GroovyAssert.shouldFail

String[] words = ['ant', 'bee']
Integer[] nums = [5, 10]

def result = words + nums           // (1)
assert result.class == Object[]
assert result*.class == [String, String, Integer, Integer] // ['ant', 'bee', 5, 
10]
words = words + nums                 // (2)
assert words.class == String[]
assert words*.class == [String, String, String, String] // ['ant', 'bee', '5', 
'10'] due to coercion

result = nums + ['cat', 'dog']         // (3)
assert result.class == Object[]
assert result*.class == [Integer, Integer, String, String]
shouldFail(ClassCastException) {
    nums = nums + ['cat', 'dog']     // (4) no auto coercion
}
{code}
I don't think it is easy to justify why (1) and (2) provide different behavior 
given the RHS is the same in both. Ditto for (3) and (4). I think the behavior 
offered in (2) and (4) should be what {{plus}} always provides.

I think we can provide an additional (set of) method(s) named {{addAll}} (or 
{{merge}} or {{union}} or similar) that returns {{Object[]}} and provides the 
current behavior shown in (1) and (3). One could imagine overloaded variants 
even allowing the array return type/component type to be specified, though I 
wouldn't go that far to start with until we see such a need.


> String[] + String[] gives Object[]
> ----------------------------------
>
>                 Key: GROOVY-6837
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6837
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.3.1
>            Reporter: Dmitry Ovchinnikov
>            Assignee: Eric Milles
>            Priority: Major
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> {code:java}
> def a1 = ["a", "b"] as String[], a2 = ["c", "d"] as String[];
> def concatenated = a1 + a2;
> java.nio.file.Paths.get("parent", concatenated)
> {code}
> ==>
> {noformat}
> groovy.lang.MissingMethodException: No signature of method: static 
> java.nio.file.Paths.get() is applicable for argument types: 
> (java.lang.String, [Ljava.lang.Object;) values: [parent, [a, b, c, d]]
> Possible solutions: get(java.lang.String, [Ljava.lang.String;), 
> get(java.net.URI), grep(), getAt(java.lang.String), wait(), any()
>       at 
> groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1373)
>       at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1359)
>       at 
> org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50)
>       at 
> org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
>       at 
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
>       at 
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
>       at ideaGroovyConsole.run(ideaGroovyConsole.groovy:4)
>       at 
> groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:258)
>       at groovy.lang.GroovyShell.run(GroovyShell.java:502)
>       at groovy.lang.GroovyShell.run(GroovyShell.java:481)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:606)
>       at 
> org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:166)
>       at 
> org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:68)
>       at 
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
>       at console.run(console.txt:25)
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to