- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Declare the type of companion objects
Mon, 2009-03-30, 20:36
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