This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Understanding scala constants

1 reply
Gordan Valjak
Joined: 2011-07-14,
User offline. Last seen 42 years 45 weeks ago.

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?

gkossakowski
Joined: 2010-03-11,
User offline. Last seen 33 weeks 5 days ago.
Re: Understanding scala constants
On 14 July 2011 13:20, Gordan Valjak <cehtunger@gmail.com> wrote:
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland