This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Declare the type of companion objects

No replies
John Nilsson
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.

Hi,

Is it possible to declare a class/trait in a manner that puts
constraints on it's companion object?

The intended effect of this being that given a class (or an instance
of that class) it is statically verified that that class has a
companion object with certain properties. For example that the
companion object has a factory method with a given signature to
produce new instances of that class.

The example that got me thinking is the Pointed type class in the
Typeclassopedia (Monad reader 13).

It seems to me that the way to really implement a Pointed trait is to
declare that classes extending the trait must have a companion object
implementing the unit method, and some means to get hold of that
companion object given a class implementing Pointed.

BR,
John

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland