Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, how can I define a companion object for a class defined in Java?

I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the companion has to be defined in the same file. Is there anything I can do about this?

Of course, I can define all my implicit conversions in another object and then bring it into scope, but this requires an extra import. Any other solution?

like image 831
Jean-Philippe Pellet Avatar asked Feb 07 '11 13:02

Jean-Philippe Pellet


People also ask

What is companion objects in Scala?

A companion object in Scala is an object that's declared in the same file as a class , and has the same name as the class. For instance, when the following code is saved in a file named Pizza.scala, the Pizza object is considered to be a companion object to the Pizza class: class Pizza { } object Pizza { }

What is the equivalent of companion object in Java?

Answer: Since companion objects are equivalent to the Static class, any variables or methods can be directly accessed from the containing class. Let's understand this with the help of a simple code example.

What is a companion class in Java?

Companion object is known as an object whose name is same as the name of the class. Or In other words, when an object and a class have the same name, then that object is known as the companion object and the class is known as companion class.

Does Java have companion objects?

Declaring a companion object In terms of Java, the members of the companion object can be accessed as static members of the class. Marking an object as a companion allows us to omit the object's name while calling its members.


2 Answers

With the Scala compiler as it stands now there is no way to define companion objects other than by putting them in the same file. The best you can do is a non-companion object with the same package and name and an extra import.

If you can think of a good way to create post-hoc companionship without breaking assumptions about encapsulation please come post on http://groups.google.com/group/scala-debate because it would clearly be a very useful feature.

like image 78
James Iry Avatar answered Sep 20 '22 10:09

James Iry


You can define your own companion object of course, which I often do in my own project-specific Predef-like arrangement. For example:

object domain {

  type TimeUnit = java.util.concurrent.TimeUnit
  object TimeUnit {
    def valueOf(s : String) = java.util.concurrent.TimeUnit.valueOf(str)
    val Millis = java.util.concurrent.TimeUnit.MILLISECONDS
    //etc
  }

Then this can be used:

import my.domain._
val tu : TimeUnit = TimeUnit.valueOf("MILLISECONDS")

But your domain.TimeUnit is a module (i.e. scala object)

like image 23
oxbow_lakes Avatar answered Sep 23 '22 10:09

oxbow_lakes