Help me with this error '=' expected, but '{' found

Newbie here😅
I use scala 3.1.3
I am trying to follow a tutorial but I am doing something wrong here

object HelloWorld extends App {
  var emp1 = new Employee("John",61709,"Male")
}

class Employee(name:String,id:Int){
  var gender:String = "NA"
  def this(name:String,id:Int,gender:String) {
  this(name,id)
  this.gender = gender
}
}

This syntax was deprecated and removed a long time ago, even before Scala 3.
As the error message suggest you should add an = there.

So

def this(name:String,id:Int,gender:String) = {
  ...
}

However, the whole code feels weird, unidiomatic, and follows bad practices.
Not sure which resource you are following to learn the language but it is at least outdated and at most poor quality.

This would be a better way to write all that code:

enum Gender:
  case Male
  case Female
  case Unknown

final case class Employee(name: String, id: Int, gender: Gender = Gender.Unknown)

object Main {
  def main(args: Array[String]): Unit = {
    val emp = Employee(
      name = "John",
      id = 61709,
      gender = Gender.Male
    )

    println(emp)
  }
}
3 Likes

Thanks a ton
I will stick to Scala’s official documentation for learning purposes.

Please don’t do that. Learning a language from documentation (even the official one) or tutorials is a very poor experience. As I like to say: it’s like trying to learn a foreign language purely from a dictionary.

Please consider taking a free online Scala class or learning from a book.

4 Likes