2

I'm having a difficult time understanding why varone, vartwo, and varthree are getting "not found" errors: "not found: value "

// The following is being added to a list of type Foo
Foo(
    varone = "stringvalue",
    vartwo = "stringvalue",
    varthree = true
)

I'm defining Foo as:

class Foo(varone : String, vartwo : String, varthree : Boolean)
{

}

I thought in Scala would take my class parameters and make fields of them?

I'm coming from a C++ background and assignments in variable constructors seem strange, but that's what the example I'm following is hinting I do. I might just revert to giving Foo a constructor and making my assignments there, but I'd like to understand this way too.

2 Answers 2

2

What you probably want is a case class

case class Foo(varone : String, vartwo : String, varthree : Boolean)

This will create all the variables for you as well as provide all the case class goodness of equals, hashes, immutability, pattern matching

Or if you don't want a case class:

class Foo(val varone : String, val vartwo : String, val varthree : Boolean)

If the field is a val, Scala will generate a getter method for it.

You can also declare it as a var, and Scala will generate both a getter and setter.

If the field doesn’t have a var / val, as in your example, Scala generates neither a getter nor setter method for the field.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to define the variables as 'val' for scala to make them as class members. Or you can use case classes.

Using 'val':

class Foo(val varone : String, val vartwo : String, val varthree : Boolean)

Using case class:

case class Foo(varone : String, vartwo : String, varthree : Boolean)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.