Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people define object extends its companion class?

Tags:

scala

I find this kind of code is very common in Lift framework, written like this:
object BindHelpers extends BindHelpers {} What does this mean?

like image 276
Sawyer Avatar asked Feb 14 '11 11:02

Sawyer


2 Answers

In this case, BindHelpers is a trait and not a class. Let foo() to be a method defined in BindHelpers, to access it you can either.

  1. Use it through the companion object: BindHelpers.foo()

  2. Mix the trait BindHelpers in a class and thus be able to access the methods inside of it.

For instance:

class MyClass extends MyParentClass with BindHelpers {
  val a = foo()
}

The same techniques is used in Scalatest for ShouldMatchers for instance.

like image 106
paradigmatic Avatar answered Nov 10 '22 14:11

paradigmatic


You can find David Pollak's answer to the same question in the liftweb group.

like image 41
Marcello Nuccio Avatar answered Nov 10 '22 14:11

Marcello Nuccio