How do you declare compile time constants in Scala? In C# if you declare
const int myConst = 5 * 5;
myConst is in-lined as the literal 25. Is:
final val myConst = 5 * 5
equivalent or is there some other mechanism/ syntax?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes,
final valis the proper syntax, with Daniel’s caveats. However, in proper Scala style your constants should be camelCase with a capital first letter.Beginning with a capital letter is important if you wish to use your constants in pattern matching. The first letter is how the Scala compiler distinguishes between constant patterns and variable patterns. See Section 15.2 of Programming in Scala.
If a
valor singleton object does not begin with an uppercase letter, to use it as a match pattern you must enclose it in backticks(``)