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?
Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.
Constants must always follow the PascalCase naming rule, according to the default style used in Scala community. Do not use CAPITAL_SNAKE_CASE or camelCase for constant names. Constants must always be defined in objects. If a constant is only used in one class, then it should be defined in its companion object.
Basic ClassClass variables are called, fields of the class and methods are called class methods. The class name works as a class constructor which can take a number of parameters. The above code defines two constructor arguments, xc and yc; they are both visible in the whole body of the class.
There are no static variables in Scala. Fields are variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object, depending on what access modifiers the field is declared with.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With