Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set a java annotation member called type in scala?

I am trying to port some java code to scala. The code uses annotations with a member called type however this is a keyword in scala. Is there a way to address this valid java member in scala?

Here is the Java code

@Component(
        name = "RestProcessorImpl",
        type = mediation // Compile error
        )
public class RestProcessorImpl {
 // impl
}

This part of the code is identical in scala except that type is a keyword so it does not compile. Is there a way to escape the type keyword?

This is also a problem with java classes with a type member

HasType.java

package spike1;

public class HasType {
    public String type() { 
        return "the type";
    }
}

UseType.scala

class UseType {
    def hasType = new HasType
    hasType.type() // Compile error
}
like image 597
iain Avatar asked May 01 '12 15:05

iain


1 Answers

You can use backticks to get around illegal identifiers:

val type = 1    // error
val `type` = 1  // fine

So with your code, you can do this:

val hasType = new HasType
hasType.`type`()   // no error
like image 197
dhg Avatar answered Oct 23 '22 21:10

dhg