Jochen Theodorou | 29 Jun 2012 18:54
Picon
Gravatar

[groovy-user] Groovy 3 and the equality of char and a String of length 1

Hi all,

there is one part in Groovy that bothers me for a long time already. 
Groovy sacrificed the char syntax for longer strings with it and instead 
in Groovy every String of length 1 is also potentially a char.

I would like to remove that and want to know your opinion on this

What I would like to allow still is

char c = stringOfLength1
def c = stringOfLength1 as char
assert "1" as char == "1"

what I don't want to allow anymore is

def foo(char c){1}
assert foo("1")==1   // Exception

and

def c = stringOfLength1

thoughts?

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
(Continue reading)

Wujek Srujek | 29 Jun 2012 19:15
Picon

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

On Fri, Jun 29, 2012 at 6:54 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Hi all,

there is one part in Groovy that bothers me for a long time already. Groovy sacrificed the char syntax for longer strings with it and instead in Groovy every String of length 1 is also potentially a char.

I would like to remove that and want to know your opinion on this

What I would like to allow still is

char c = stringOfLength1

So you want to allow the above, which is the same as:
char c = "1"

but not:

def foo(char c){1}
assert foo("1")==1   // Exception
 
?
So we will be able to do an assignment a variable string-of-1-char to a char, but not call a method that takes a char with a string-of-1-char argument? Seems strange.

wujek
Jochen Theodorou | 29 Jun 2012 20:55
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

Am 29.06.2012 19:15, schrieb Wujek Srujek:
[...]
> So we will be able to do an assignment a variable string-of-1-char to a
> char, but not call a method that takes a char with a string-of-1-char
> argument? Seems strange.

we have a pattern like that in Groovy, take for example

int[] i = [1,2,3]

this is a List to int[] conversion. I always say an assignment in Groovy 
involves an implicit cast, thus the assignment above is

int[] i = (int[]) [1,2,3]

and the cast is doing the assignment. A method call in Groovy contains 
no such cast, but still the widening rules of Groovy of course apply

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

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

    http://xircles.codehaus.org/manage_email

Wujek Srujek | 29 Jun 2012 22:26
Picon

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

I would say that it is a very misleading behavior, then. For me (maybe I am naive and not educated enough) a method call involves multiple assignments (one for each argument, to the parameters) and I would expect it to use the same rules.


wujek

On Fri, Jun 29, 2012 at 8:55 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Am 29.06.2012 19:15, schrieb Wujek Srujek:
[...]

So we will be able to do an assignment a variable string-of-1-char to a
char, but not call a method that takes a char with a string-of-1-char
argument? Seems strange.

we have a pattern like that in Groovy, take for example

int[] i = [1,2,3]

this is a List to int[] conversion. I always say an assignment in Groovy involves an implicit cast, thus the assignment above is

int[] i = (int[]) [1,2,3]

and the cast is doing the assignment. A method call in Groovy contains no such cast, but still the widening rules of Groovy of course apply


bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org




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

  http://xircles.codehaus.org/manage_email



Jochen Theodorou | 29 Jun 2012 23:38
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

Am 29.06.2012 22:26, schrieb Wujek Srujek:
> I would say that it is a very misleading behavior, then. For me (maybe I
> am naive and not educated enough) a method call involves multiple
> assignments (one for each argument, to the parameters) and I would
> expect it to use the same rules.

if we want Groovy to behave for method calls as in assignments, we we 
have to change a lot.

the idea of having an assignment for a method call, is a quite classic 
"visualization" of what happens. So I can very well understand that 
people are maybe not happy about the current situation.

bye blackdrag

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

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 29 Jun 2012 23:59
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

Am 29.06.2012 23:38, schrieb Jochen Theodorou:
> Am 29.06.2012 22:26, schrieb Wujek Srujek:
>> I would say that it is a very misleading behavior, then. For me (maybe I
>> am naive and not educated enough) a method call involves multiple
>> assignments (one for each argument, to the parameters) and I would
>> expect it to use the same rules.
>
> if we want Groovy to behave for method calls as in assignments, we we
> have to change a lot.

I wonder how many such conversions we actually have..

Collection to Array is surely one such  - plus String, GString, File, 
Enum, and funny 1-element list to array conversions... surely almost no 
one knows that this

class X{}
def x = new X()
Object[] foo = x
assert foo[0] == x

will not fail in Groovy.

Boolean conversions is another - method calls don't do Groovy truth.

We have a conversion for HashSet and all its subclasses
HashSet foo = [1,2]

Just anything is assignable to String, that is only for GString the case 
in method calls

Anything can be assigned to Class, toString and Class#forName are used 
to accomplish that, method calls don't do that

We have a String to Enum conversion using Enum#valueOf

And finally we have the last resort of the Groovy style cast, the 
invocation of the target's constructor using the cast argument as 
argument for the constructor, as long as it is a Collection or Map. 
Example:

class X{
   X(x){}
}
X y = [1]

there are probably more cases (apart from the already mentioned char 
conversion), but that is what I can easily see form the castToType code.

Enabling them all for method calls would make many many method calls 
ambigious. So I actually would like to go the other way and remove some 
of those conversions by discussing each of them

bye blackdrag

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

    http://xircles.codehaus.org/manage_email

Gavin Grover | 1 Jul 2012 11:15
Picon
Favicon

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

----- Original Message -----

> From: Jochen Theodorou <blackdrag <at> gmx.org>

> what I don't want to allow anymore is

> def c = stringOfLength1

> 
> thoughts?

All of the following work in Groovy 2.0.

  def c="z"
  assert c == "z"
  assert c == ("z" as char)
  assert c.getClass() == String
  assert c in String

Are you saying you don't want the 2nd assert to work any longer, so that...

  assert c != ("z" as char)

works in Groovy3 ?

Cheers,
Gavin Grover
blog: http://groovy.codeplex.com/wikipage?title=Blog02
email: gvgrover@...

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

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 1 Jul 2012 23:53
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

Am 01.07.2012 11:15, schrieb Gavin Grover:
> ----- Original Message -----
[...]
> All of the following work in Groovy 2.0.
>
>    def c="z"
>    assert c == "z"
>    assert c == ("z" as char)
>    assert c.getClass() == String
>    assert c in String
>
> Are you saying you don't want the 2nd assert to work any longer, so that...

def c="z" makes c a String. I don't to change that at all. Therefore the 
second assert is not influenced too.

actually I found out we have conversion rules for Strings of length 1, 
but we don't apply them. I thought

def foo(char c){1}
assert foo("z")==1

works, but it does not!

The situation I wanted to reach is already the case it seems.

bye blackdrag

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

    http://xircles.codehaus.org/manage_email

Russel Winder | 2 Jul 2012 18:57
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

On Fri, 2012-06-29 at 18:54 +0200, Jochen Theodorou wrote:
> Hi all,
> 
> there is one part in Groovy that bothers me for a long time already. 
> Groovy sacrificed the char syntax for longer strings with it and instead 
> in Groovy every String of length 1 is also potentially a char.
> 
> I would like to remove that and want to know your opinion on this

Python, like Groovy, has the feature that there are no characters only
strings, this hasn't caused problems. What is the reason for wanting to
re-introduce characters to Groovy – they haven't actually gone away per
se since the JVM Character and char types are still available in Groovy.

> What I would like to allow still is
> 
> char c = stringOfLength1
> def c = stringOfLength1 as char
> assert "1" as char == "1"
> 
> what I don't want to allow anymore is
> 
> def foo(char c){1}
> assert foo("1")==1   // Exception
> 
> and
> 
> def c = stringOfLength1

What can be wrong with the above c is a reference to an object and a
string of length 1 is a perfectly valid object. Or am I missing
something?

--

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder <at> ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@...
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Jochen Theodorou | 3 Jul 2012 10:14
Picon
Gravatar

Re: [groovy-user] Groovy 3 and the equality of char and a String of length 1

Am 02.07.2012 18:57, schrieb Russel Winder:
> On Fri, 2012-06-29 at 18:54 +0200, Jochen Theodorou wrote:
>> Hi all,
>>
>> there is one part in Groovy that bothers me for a long time already.
>> Groovy sacrificed the char syntax for longer strings with it and instead
>> in Groovy every String of length 1 is also potentially a char.
>>
>> I would like to remove that and want to know your opinion on this
>
> Python, like Groovy, has the feature that there are no characters only
> strings, this hasn't caused problems. What is the reason for wanting to
> re-introduce characters to Groovy – they haven't actually gone away per
> se since the JVM Character and char types are still available in Groovy.

believe me, I am perfectly fine with Groovy not knowing chars at all. 
But to be able to call methods taking char we have to have defined 
transformation ways, some automatic, some not. The question here is how 
many of them should be automatic, like for example the GString to String 
conversion. You can call a method taking a String and call it using a 
GString.

Funny fact is, that I was assuming we would do this kind of conversion 
for Strings of length 1 to char. But we do not... Silly me. So the 
thread is partially nonsense, unless we want to talk about the implicit 
casting behaviour in Groovy. But even then a new thread might be better.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

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

    http://xircles.codehaus.org/manage_email

Aaron Babcock | 4 Jul 2012 20:16
Picon

[groovy-user] groovysh throws immediate exception when xmlParserAPIs-2.6.2.jar is in classpath

I ran into this recently and it confused me for a while. Just having this jar in the classpath will prevent you from getting to the groovysh prompt. Is this a bug?

curl -O "http://repo1.maven.org/maven2/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar"; 
~/groovy-1.8.6/bin/groovysh -cp "xmlParserAPIs-2.6.2.jar";
~/groovy-2.0.0/bin/groovysh -cp "xmlParserAPIs-2.6.2.jar";



java.lang.reflect.InvocationTargetException
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.tools.GroovyStarter.rootLoader(GroovyStarter.java:106)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
Caused by: javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.SAXParserFactoryImpl not found
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at groovy.xml.FactorySupport$2.run(FactorySupport.java:56)
at java.security.AccessController.doPrivileged(Native Method)
at groovy.xml.FactorySupport.createFactory(FactorySupport.java:33)
at groovy.xml.FactorySupport.createSaxParserFactory(FactorySupport.java:54)
at groovy.util.XmlParser.<init>(XmlParser.java:70)
at groovy.util.XmlParser.<init>(XmlParser.java:66)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:186)
at org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar$_register_closure1.doCall(XmlCommandRegistrar.groovy:52)
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.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
at groovy.lang.Closure.call(Closure.java:410)
at groovy.lang.Closure.call(Closure.java:423)
at org.codehaus.groovy.runtime.IOGroovyMethods.withReader(IOGroovyMethods.java:1074)
at org.codehaus.groovy.runtime.IOGroovyMethods.withReader(IOGroovyMethods.java:1123)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.withReader(ResourceGroovyMethods.java:1781)
at org.codehaus.groovy.runtime.dgm$845.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
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:116)
at org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar.register(XmlCommandRegistrar.groovy:51)
at org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar$register.call(Unknown Source)
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:116)
at org.codehaus.groovy.tools.shell.Groovysh$_createDefaultRegistrar_closure3.doCall(Groovysh.groovy:80)
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.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
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:116)
at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:74)
at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:85)
at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:89)
at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at org.codehaus.groovy.tools.shell.Main.main(Main.groovy:125)
... 6 more

java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:39)
at java.lang.Runtime.addShutdownHook(Runtime.java:192)
at jline.UnixTerminal.initializeTerminal(UnixTerminal.java:91)
at jline.Terminal.setupTerminal(Terminal.java:75)
at jline.Terminal.getTerminal(Terminal.java:26)
at jline.Terminal$getTerminal.call(Unknown Source)
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:112)
at org.codehaus.groovy.tools.shell.AnsiDetector.call(Main.groovy:210)
at org.codehaus.groovy.tools.shell.AnsiDetector.call(Main.groovy)
at org.fusesource.jansi.Ansi.isDetected(Ansi.java:155)
at org.fusesource.jansi.Ansi$2.initialValue(Ansi.java:166)
at org.fusesource.jansi.Ansi$2.initialValue(Ansi.java:163)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
at java.lang.ThreadLocal.get(ThreadLocal.java:131)
at org.fusesource.jansi.Ansi.isEnabled(Ansi.java:175)
at org.fusesource.jansi.Ansi.ansi(Ansi.java:179)
at org.fusesource.jansi.AnsiRenderer.render(AnsiRenderer.java:103)
at org.fusesource.jansi.AnsiRenderer.render(AnsiRenderer.java:92)
at org.fusesource.jansi.AnsiRenderWriter.write(AnsiRenderWriter.java:56)
at java.io.PrintWriter.print(PrintWriter.java:559)
at java.io.PrintWriter.println(PrintWriter.java:695)
at java_io_PrintWriter$println$0.call(Unknown Source)
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:116)
at org.codehaus.groovy.tools.shell.Main$_main_closure2.doCall(Main.groovy:118)
at org.codehaus.groovy.tools.shell.Main$_main_closure2.doCall(Main.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.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
at groovy.lang.Closure.call(Closure.java:410)
at groovy.lang.Closure.call(Closure.java:404)
at groovy.lang.Closure.run(Closure.java:488)
at java.lang.Thread.run(Thread.java:680)
WARNING: Abnormal JVM shutdown detected

Gmane