Kevin Meredith
Kevin Meredith

Reputation: 41939

Case Class without Parameters

The Scala Docs on object comment:

In fact, a case class with no type parameters will by default create a singleton object of the same name, with a Function* trait implemented.

What does the with a Function* trait implemented mean?

Upvotes: 2

Views: 711

Answers (1)

marios
marios

Reputation: 8996

* is the cardinality of the case class; that is, the number of arguments it takes.

Putting it together:

case class Foo(a: Int, b: Long)

Represents code that looks like this:

class Foo(val a: Int, val b: Long) 

object Foo extends Function2[Int,Long,Foo] {
   def apply(a: Int, b: Long): Foo = new Foo(a,b)
}

The above code is not complete, case class creates a lot of other helper functions like pretty printing, unapply for pattern matching, structural equality tests, etc.

Upvotes: 6

Related Questions