Soumya Simanta
Soumya Simanta

Reputation: 11741

Trying to understand extends syntax in Scala

I recently saw the following piece of code and I'm trying to understand the syntax.

What I don't understand is object Widget extends ((Int, String, DateTime) => Widget ) part. Can someone explain the syntax and what's happening here?

case class Widget(
  id      : Int,
  name    : String,
  created : DateTime = now
)

object Widget extends ((
  Int,
  String,
  DateTime
) => Widget) { ..... }

Upvotes: 1

Views: 72

Answers (1)

joescii
joescii

Reputation: 6533

The short answer is your object is extending a Function type which takes as input the triplet of types Int, String, and DateTime and returns a Widget. Hence, you will need to override the apply(Int, String, DateTime) function. Once you've done that, you will have made a function named Widget. This in fact is what a case class implements for you behind the scenes. In this case, I believe your Widget object's definition of the function will take precedence over the default case class one.

Upvotes: 3

Related Questions