Comparing two characters in Xtend

295 views
Skip to first unread message

Hemal Bavishi

unread,
Mar 2, 2015, 7:44:30 AM3/2/15
to xtend...@googlegroups.com
Hello,

I found strange issue while comparing character in my program. Initially I used following way but it didn't work as it's taking 'P' as string

«IF(inputDataArgumentType == 'P' «{appDataType = "*AppData" ""
«ELSE» «{appDataType = "AppData" ""

I found workaround by converting String to charArray and later used first instance or assign "P" to char type of variable and compare it later. 

«IF(inputDataArgumentType == 'P'.toCharArray.get(0))» «{appDataType = "*AppData" ""
«ELSE» «{appDataType = "AppData" ""

«val char pointer = "P"»
«IF(inputDataArgumentType == pointer «{appDataType = "*AppData" ""
«ELSE» «{appDataType = "AppData" ""
«ENDIF»

I am just wondering whether this is the way it's expected to behave or it's a bug?

Best Regards,
Hemal Bavishi


Typhoon Storm

unread,
Mar 2, 2015, 9:12:30 PM3/2/15
to xtend...@googlegroups.com
Currently there is no char literal in Xtend. Join discussion at https://bugs.eclipse.org/bugs/show_bug.cgi?id=412383

I wrote a little helper method to overcome this:

    def static char !(String str) {
        if (str.length != 1)
            throw new RuntimeException("A char literal string must have length of 1! str = " + str)
        return str.charAt(0)
    }

Usage:

    def static void test(char x) {}
    
    def static void main(String[] args) {
        val x = !'a'
        println(x)
        test(x)
    }

It is good for me for its concise, that is you don't need to write 'a'.charAt(0) or assign 'a' to a char variable first. 
Will be good if this way is ok for all and can be added to Xtend's library.

Sven Efftinge

unread,
Mar 3, 2015, 1:56:21 AM3/3/15
to xtend...@googlegroups.com
Hi Hemal,

as Typhoon already pointed out, there is no real char lteral atm.
The only thing that is available is auto cooercion if the expected type is char.

e.g.:

  val char x = ’c’

will be translated to

  final char x = ‘c’;

while

  val x = ‘c’

will be translated to

  final String x = “c”;

This works well in many cases but with generic methods like equals it doesn’t.

Sven

@Typhoon: I think if your helper method would have a char parameter, the compiler would do the conversion and checking for you.

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Typhoon Storm

unread,
Mar 3, 2015, 4:26:48 AM3/3/15
to xtend...@googlegroups.com, efft...@itemis.de
Hi Sven,

Thanks for the information you gave, and I tried it out and to my surprise, it didn't work as expected. Code example is as follows:

class CharLiteralTest {
    def static char !(char ch) { // Did you mean this way?
        return ch
    }

//    def static char !(String str) {
//        if (str.length != 1)
//            throw new RuntimeException("A char literal string must have length of 1! str = " + str)
//        return str.charAt(0)
//    }
    
    def static void test(char x) {}
    
    def static void main(String[] args) {
        val x = !'a' // Compile error: ! cannot be resolved.
        val y = operator_not('a') // OK
        println(x)
        test(x)
        println(y)
        test(y)
    }
}

I also did a few other overloaded operator tests with the same result. So it seems auto coercion doesn't work for operator overloading.
Reply all
Reply to author
Forward
0 new messages