Reserved Keywords

PS : 51
Some Java methods(Example : java.util.Scanner.match) are reserved keywords in Scala. Use back-ticks(``) to specify those method names.

scala> import java.util.Scanner
import java.util.Scanner

scala> val scanner:Scanner = new Scanner("This is a demo")
scanner: java.util.Scanner = java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

scala> scanner.hasNext("This")
res57: Boolean = true

scala> // 'match' is a reserved keyword and will throw error

scala> scanner.match()
<console>:1: error: identifier expected but 'match' found.
       scanner.match()
               ^
<console>:1: error: '{' expected but '(' found.
       scanner.match()
                    ^

scala> // Backtick is used around the keyword 'match'

scala> scanner.`match`()
res58: java.util.regex.MatchResult = java.util.regex.Matcher[pattern=This region=0,14 lastmatch=This]

No comments:

Post a Comment