Why do people define object extends its companion class?

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


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.

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

It's interesting for an object to extend its companion class because it will have the same type as the class.

If object BindHelpers didn't extend BindHelpers, it would be of type BindHelpers$.

It might be that the pattern here is other. I don't know Lift to answer this, but there's a problem with object in that they are not mockable. So, if you define everything in a class, which can be mocked, and then just makes the object extend it, you can mock the class and use it instead of the object inside your tests.

Category:scala Time:2011-02-14 Views:1
Tags: scala

Related post

  • Why do case class companion objects extend FunctionN? 2010-06-15

    When you create a case class, the compiler creates a corresponding companion object with a few of the case class goodies: an apply factory method matching the primary constructor, equals, hashCode, and copy. Somewhat oddly, this generated object exte

  • How to convert a literal object into user defined object in javascript? 2008-11-24

    Writing a ton of web applications leveraging JSON/AJAX, I find myself returning tons literal javascript objects (JSON). For example, I may be request all the Cats from GetCats.asp. It would return: [ { 'id': 0, 'name': 'Persian' }, { 'id': 1, 'name':

  • Is it possible to add something to the prototype of an literally defined object in Javascript? 2011-06-14

    So if I was to create an object literal like this.. var person1 = { age : 28, name : 'Pete' } Then I add a function to the object... person1.sayHelloName = function(){ return 'Hello ' + this.name; }; alert(person1.sayHelloName()); Im wondering where

  • Parsing XML data to user defined objects, SAX or DOM 2011-11-14

    I want to create a simple function as follow: vector <User> convertXMLDataToUserList (string xmlData) { …. } Let's say that the xmlData is something as follow: <users> <user> <firstname>ABC</firstname> <lastname>DE

  • Defining objects in Javascript 2012-03-02

    I want to define the following object in javascript: var property = 'foo'; var value = 'bar'; var obj = { property: value; }; This will result the following: obj.property <-> 'bar'; obj.foo <-> undefined This is normal, but how to define

  • Defining objects when using Jaxer 2008-09-20

    I've been playing with Jaxer and while the concept is very cool I cannot figure out how to define objects that are available on both the client and the server. None of the examples I can find define objects at all. I'd like to be able to define an ob

  • How do I get the user defined object type given the class name? 2009-05-01

    If I have the class name of the user defined object as a string how do I use it in a generic function as Type of the object ? SomeGenericFunction(objectID); --------------Solutions------------- If you have a string, then the first thing to do is to u

  • Get a user-defined object in Javascript 2009-09-10

    I have a script which creates a user-defined object like this: obj = new Tree(); This object is always present and interacting with the user. I want to create a button on the page which, if pressed, should retrieve this object, make some changes to t

  • is there any api provided by dot net platform to convert a string or bytres array into user defined object? 2009-11-19

    Suppose i have input array byte A[50]; i have put three diffrent data types values in array as below string of length 42 bytes(converted into binary) long with length 4 bytes(converted into binary) float with length of 4 bytes(converted into binary)

  • Android: Saving ArrayList of User Defined Objects between onPause and onResume 2010-08-10

    I am working on an application where i am saving the state of an application in an ArrayList. Now, to save this state, i tried to use Serialization. But, somewhere in the user defined object, i am using Button, which is not letting me serialize the e

  • Bundle array of json/File/user-defined objects in Android 2010-10-21

    how to bundle array of File objects or array of Json objects or array of user-defined objects in one activity and send it to another and how to access them correctly. ? Primitive types like string is quite easy with Intent in = new Intent(); Bundle b

  • bisect and lists of user defined objects (python 3) 2010-11-15

    Before python 3, I used bisect to insert user-defined objects into a list. bisect was happy with this because my user-defined object had a def __cmp__ that defined how to compare the objects. I've read the rationale for not supporting cmp in python 3

  • How to pass user defined objects with a CXF REST client? 2011-08-21

    I'm developing an android application that needs to connect to a remote database through a CXF web service.I have tried using Soap but due to various problems left the option and went for a light weight REST based service(by adding the annotations to

  • How to pass an array to an user defined object or convert non-OO code to OO? javascript 2011-11-22

    I really have trouble with OO coding in js. I have written a piece of code which rotates through 3 divs, and pauses on hover of any div. This code is just regular js using an array/json as the input. the code is a bit long so sorry about that. I just

  • Android-How to make moving onscreen objects extend Button 2012-01-20

    In an app Im working on I have a class that extends a surface view and another that handles all calculations to figure out where and when objects should be drawn onscreen. Right now I have a sort of hacky way of registering screen clicks where a list

  • JQuery plugin architecture, where to define objects and variables life cycle? 2012-04-26

    I m writting a jquery plugin to render images in a canvas. The ultimate goal is to achive something like var myImageSource = new ImageSource(path,width,height); $("#myCanvas").Render({"source" : myImageSource}); The plugin requiers several classes, h

  • HTML5 canvas with own defined objects 2014-04-22

    I have a question concerning the canvas and self defined objects on a canvas. The self defined objects on the canvas are a javascript class with different attributes to make it possible to draw that javascript object several times on the canvas. So n

  • Type casting for user defined objects 2009-07-18

    Hi Just like we do with __ToString, is there a way to define a method for casting? $obj = (MyClass) $another_class_obj; --------------Solutions------------- There is no need to type cast in php. Edit: Since this topic seems to cause some confusion, I

  • JiBX binding - root object extends an object 2009-10-17

    Is even possible to bind an root object with super fields? I have quite complicated entity (entities) to bind. I am already lost with it :) This is my code situation: public class CoreRestWrapper { private Integer page; private Integer perPage; } pub

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.707 (s). 13 q(s)