The escape character does not work in certain case.

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

The escape character does not work in certain case.

Huxi LI
Hi all,

I got the following error
groovy> println Character.isLetter('c')
groovy> go
Caught: groovy.lang.MissingMethodException: No signature of method java.lang.Cha
racter.isLetter() is applicable for argument types: (java.lang.String) values: {
"c"}
        at CommandLine.run(CommandLine.groovy)

This is because Groovy consider 'c' as a string. This is striking for Java developers.
I am wondering if someone has created such type of bug, otherwise I would like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for instance,
   
        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Huxi LI
Sorry for the title of the message, it is a little confusing.
It should be 'Problems with character'



On 12/13/05, Huxi LI <[hidden email]> wrote:
Hi all,

I got the following error
groovy> println Character.isLetter('c')
groovy> go
Caught: groovy.lang.MissingMethodException: No signature of method java.lang.Cha
racter.isLetter() is applicable for argument types: (java.lang.String) values: {
"c"}
        at CommandLine.run(CommandLine.groovy)

This is because Groovy consider 'c' as a string. This is striking for Java developers.
I am wondering if someone has created such type of bug, otherwise I would like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for instance,
   
        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]



--
Huxi LI
E-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Huxi LI
What a day!  I means
 
    char c = /\/
    println c

does not work. It is normal that
    char c = '\'
does not work!
 



On 12/13/05, Huxi LI <[hidden email]> wrote:
Sorry for the title of the message, it is a little confusing.
It should be 'Problems with character'




On 12/13/05, Huxi LI <[hidden email]> wrote:
Hi all,

I got the following error
groovy> println Character.isLetter('c')
groovy> go
Caught: groovy.lang.MissingMethodException: No signature of method java.lang.Cha
racter.isLetter() is applicable for argument types: (java.lang.String) values: {
"c"}
        at CommandLine.run(CommandLine.groovy)

This is because Groovy consider 'c' as a string. This is striking for Java developers.
I am wondering if someone has created such type of bug, otherwise I would like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for instance,
   
        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]



--

Huxi LI
E-mail: [hidden email]



--
Huxi LI
E-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: Re: The escape character does not work in certain case.

Dierk König
In reply to this post by Huxi LI
"c" and 'c' are both strings
'c' as char
is of the primitive char type
'c' as Character
is the according object.

BTW: such questions should go to 'groovy-user' not dev.

cheers
Mittie

-----Original Message-----
From: Huxi LI [mailto:[hidden email]]
Sent: Dienstag, 13. Dezember 2005 11:21
To: [hidden email]
Subject: [groovy-dev] Re: The escape character does not work in certain
case.


Sorry for the title of the message, it is a little confusing.
It should be 'Problems with character'




On 12/13/05, Huxi LI <[hidden email]> wrote:
Hi all,

I got the following error

groovy> println Character.isLetter('c')
groovy> go

Caught: groovy.lang.MissingMethodException: No signature of method
java.lang.Cha
racter.isLetter() is applicable for argument types: (java.lang.String)
values: {
"c"}
        at CommandLine.run(CommandLine.groovy)


This is because Groovy consider 'c' as a string. This is striking for Java
developers.
I am wondering if someone has created such type of bug, otherwise I would
like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for
instance,

        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]



--
Huxi LI
E-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Guillaume Laforge
Administrator
In reply to this post by Huxi LI
Hello,

On 13/12/05, Huxi LI <[hidden email]> wrote:

>  I got the following error
>
> groovy> println Character.isLetter('c')
>  groovy> go
>
> Caught: groovy.lang.MissingMethodException: No signature of
> method java.lang.Cha
>  racter.isLetter() is applicable for argument types: (java.lang.String)
> values: {
>  "c"}
>          at CommandLine.run(CommandLine.groovy)
>
>  This is because Groovy consider 'c' as a string. This is striking for Java
> developers.
>  I am wondering if someone has created such type of bug, otherwise I would
> like
>  to add one (If you think it is a bug !)

To create a character, you have to use String to Character coercion through:

"c" as Character

So your code becomes:

groovy> println Character.isLetter('c' as Character)

>  In fact, I have many problems to manipulate characters with Groovy, for
> instance,
>
>          char c = '\'
>
>  does not work either!

Did you try escaping the backslash?

char c = '\\'

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

RE: Re: The escape character does not work in certain case.

Dierk König
In reply to this post by Huxi LI
try
char c = /\/ as char

cheers
Mittie


-----Original Message-----
From: Huxi LI [mailto:[hidden email]]
Sent: Dienstag, 13. Dezember 2005 11:31
To: [hidden email]
Subject: [groovy-dev] Re: The escape character does not work in certain
case.


What a day!  I means

    char c = /\/
    println c

does not work. It is normal that
    char c = '\'
does not work!





On 12/13/05, Huxi LI <[hidden email]> wrote:
Sorry for the title of the message, it is a little confusing.
It should be 'Problems with character'





On 12/13/05, Huxi LI <[hidden email]> wrote:
Hi all,

I got the following error

groovy> println Character.isLetter('c')
groovy> go

Caught: groovy.lang.MissingMethodException: No signature of method
java.lang.Cha
racter.isLetter() is applicable for argument types: (java.lang.String)
values: {
"c"}
        at CommandLine.run(CommandLine.groovy)


This is because Groovy consider 'c' as a string. This is striking for Java
developers.
I am wondering if someone has created such type of bug, otherwise I would
like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for
instance,

        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]




--

Huxi LI
E-mail: [hidden email]



--
Huxi LI
E-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Jochen Theodorou
In reply to this post by Huxi LI
Huxi LI schrieb:

> Hi all,
>
> I got the following error
> groovy> println Character.isLetter('c')
> groovy> go
> Caught: groovy.lang.MissingMethodException: No signature of method
> java.lang.Cha
> racter.isLetter() is applicable for argument types: (java.lang.String)
> values: {
> "c"}
>         at CommandLine.run(CommandLine.groovy)
>
> This is because Groovy consider 'c' as a string. This is striking for
> Java developers.
> I am wondering if someone has created such type of bug, otherwise I
> would like to add one (If you think it is a bug !)

Groovy doesn't know characters by default, only Strings. but strings of
length 1 can be handled as character. But this is only for declarations.
due to the dynamic nature of groovy it is not possible to convert a
String to a character by the compiler here. What you can do are 3 things:

1.) assign to a variable of type char
2.) cast to char
3.) use "as char"

Note: instead of "char" you can also use "Character"

> In fact, I have many problems to manipulate characters with Groovy, for
> instance,
>    
>         char c = '\'
>
> does not work either!

char c = '\\'
assert c.class == Character
def s = '\\'
assert s.class == String
assert c == s


bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: Re: The escape character does not work in certain case.

Huxi LI
In reply to this post by Dierk König


On 12/13/05, Dierk Koenig <[hidden email]> wrote:
"c" and 'c' are both strings
'c' as char
is of the primitive char type
'c' as Character
is the according object.

BTW: such questions should go to 'groovy-user' not dev.

Sorry if it disturb you for such type of messages.
 

cheers
Mittie

-----Original Message-----
From: Huxi LI [mailto:[hidden email]]
Sent: Dienstag, 13. Dezember 2005 11:21
To: [hidden email]
Subject: [groovy-dev] Re: The escape character does not work in certain
case.


Sorry for the title of the message, it is a little confusing.
It should be 'Problems with character'




On 12/13/05, Huxi LI <[hidden email]> wrote:
Hi all,

I got the following error

groovy> println Character.isLetter('c')
groovy> go

Caught: groovy.lang.MissingMethodException: No signature of method
java.lang.Cha
racter.isLetter () is applicable for argument types: (java.lang.String)
values: {
"c"}
        at CommandLine.run(CommandLine.groovy)


This is because Groovy consider 'c' as a string. This is striking for Java
developers.
I am wondering if someone has created such type of bug, otherwise I would
like
to add one (If you think it is a bug !)

In fact, I have many problems to manipulate characters with Groovy, for
instance,

        char c = '\'

does not work either!

bye

-
Huxi LI
E-mail: [hidden email]



--
Huxi LI
E-mail: [hidden email]




--
Huxi LI
E-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Martin C. Martin
In reply to this post by Huxi LI


Huxi LI wrote:
>
> This is because Groovy consider 'c' as a string. This is striking for
> Java developers.
> I am wondering if someone has created such type of bug, otherwise I
> would like
> to add one (If you think it is a bug !)

In Groovy, like in Python and other dynamic languages, you can enclose
strings in either single or double quotes.  This makes it easy to have
double quotes in a string:

def myString = 'You say "goodbye," and I say "hello."'

If you want single quotes in the string, you can enclose it in double
quotes.  If you want both, you'll have to escape one or the other.

Best,
Martin
Reply | Threaded
Open this post in threaded view
|

Re: The escape character does not work in certain case.

Huxi LI

I totally agree with you about mixing single and double quotes
and I actually use such mixing extensively in my groovy
scripts (they works perfectly in production environment for our large projects).
 
But I am wondering why not "autoboxing" a single character
string to a character (If there does not have one with string as parameter)
in method call; this will make calling Java method more friendly. For example:

Java Class:  

public class A{
      public void say(char  c){
             System.out.println("It is a character:" + c);
      }
      public void say(String c){
             System.out.println("It is a String:" + c);
      }
}


public class B{
      public void say(char  c){
             System.out.println("It is a character:" + c);
      }
}


Groovy Script:

        new A().say('x');    //  Call say(String s)
        new B().say('x');    //  Call say(char c), and not throw execption


Anyway, thanks for explanation.

Best regards,

Huxi

On 12/13/05, Martin C. Martin <[hidden email]> wrote:


Huxi LI wrote:
>
> This is because Groovy consider 'c' as a string. This is striking for
> Java developers.
> I am wondering if someone has created such type of bug, otherwise I
> would like
> to add one (If you think it is a bug !)

In Groovy, like in Python and other dynamic languages, you can enclose
strings in either single or double quotes.  This makes it easy to have
double quotes in a string:

def myString = 'You say "goodbye," and I say "hello."'

If you want single quotes in the string, you can enclose it in double
quotes.  If you want both, you'll have to escape one or the other.

Best,
Martin



--
Huxi LI
E-mail: [hidden email]