Use Class Variables As Constants In Scala
Asked Answered
H

3

23

I'm working to learn Scala--coming from a C++ background. I am trying to write a small class for a task tracking app I'm hacking together to help me to learn how to code Scala.

This seems as if it should be simple but for some reason it's eluding me:

package com.catenacci.tts

class Task(val ID:Int, val Description:String) {
 val EmptyID = 0
 val EmptyDescription = "No Description"

 def this() = this(EmptyID,EmptyDescription)
 def this(ID:Int)={
   this(ID,EmptyDescription)
 }
 def this(Description:String)={
   this(EmptyID,Description)
 }
}

I'm trying to provide three constructors: Task(ID, Description), Task(ID), Task(Description). In the latter 2 cases I want to initialize the values to constant values if one of the values isn't provided by the caller. And I want to be able to check this outside of the class for unit testing purposes. So I figured putting in two public vals would allow me to check from outside of the class to make sure that my state is what I expect.

However, for some reason this code will not compile. I get the following error:

error: not found: value EmptyID

and

error: not found: value EmptyDescription

So what am I missing? I'm working through "Programming in Scala" so if there's a simple answer to this question, please give me page numbers. I don't mind reading but going by the code on page 60 and page 62, I can't see why this code is failing.

I'm guessing it has something to do with the fact that these are constructor methods and that possibly the two vals are not initialized until the end of the constructors. If that's the case is there some way to get the effect I'm looking for?

Hesperidin answered 29/4, 2009 at 16:32 Comment(1)
See also SIP 23 or 42.type: https://mcmap.net/q/584619/-scala-singleton-factories-and-class-constantsCotangent
G
46

You can define the constants in a companion object:

object Task { 
 val EmptyID = 0
 val EmptyDescription = "No Description"
}

And then reference them as Task.EmptyID and Task.EmptyDescription.

I think Scala 2.8 has support for default values.

Godly answered 29/4, 2009 at 17:26 Comment(0)
T
8

See Germán for the answer. This happens because a constructor is technically part of the static scope. In other words, the constructor cannot access any instance members because the instance hasn't been created yet. Any "class members" are actually instance members, which is why the code in the question does not work. Germán's answer fixes this by moving the two relevant values into the companion object, which effectively makes them static members of the Task class (not really, but you can think of it that way).

Tetzel answered 29/4, 2009 at 22:35 Comment(2)
Thanks--it's good to know why this didn't work as well as knowing how to fix it.Hesperidin
Yeah. For a while I was wondering why this wasn't a problem in Java with the classic "public static final" - it's because of the static.Coffelt
N
1

In "Programming in Scala", see section 6.7 where the chaining of constructor calls is explained. The primary constructor is described as "the single point of entry of a class".

Ninnyhammer answered 3/5, 2009 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.