- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Understanding scala constants
Thu, 2011-07-14, 12:20
Hi everyone!
I just found some interesting behavior of scala constants, that was
not intuitively clear to me.
Example:
object SomeObject {
//we want to assign some boolean constants depending on current user
val userName = getUserName()
val (Debug, Admin) =
username match {
case "user1" => (true, true)
case "user2" => (true, false)
case "user3" => (false, false)
}
}
That produces compile error: "not found: value Debug not found: value:
Production", but only if Debug and Admin are defined with first
capital letter. If i put: "...val(debug, admin) =..." everything works
fine.
If i define some Trait that have abstract constants Debug and
Production:
trait SomeTrait {
val Debug: Boolean
val Admin: Boolean
}
and extend SomeObject from first example with that trait i get compile
error saying those two values are not defined. Again everything works
if they are declared with lower first letter.
In fact in both cases with lower first letter i can easily define
constants after that:
val Debug = debug
val Admin = admin,
so what i'm trying to understand here is what is actualy happening,
and how scala handles variables and constants regarding first letter.
I'm assuming it has something with scala extractors, but not sure what
exactly?
You are using patten matching here. See this:
http://scala-programming-language.1934581.n4.nabble.com/Capital-letters-cause-tuple-unpacking-assignation-to-fail-td1942041.html
for details.
--
Grzegorz Kossakowski