Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala type alias including companion object [beginner]

I'd like to write a type alias to shorten, nice and encapsulated Scala code. Suppose I got some collection which has the property of being a list of maps, the value of which are tuples. My type would write something like List[Map[Int, (String, String)]], or anything more generic as my application allows it. I could imagine having a supertype asking for a Seq[MapLike[Int, Any]] or whatever floats my boat, with concrete subclasses being more specific.

I'd then want to write an alias for this long type.

class ConcreteClass {
  type DataType = List[Map[Int, (String, String)]]
  ...
}

I would then happily use ConcreteClass#DataType everywhere I can take one, and use it.

Now suppose I add a function

def foo(a : DataType) { ... }

And I want to call it from outside with an empty list. I can call foo(List()), but when I want to change my underlying type to be another type of Seq, I'll have to come back and change this code too. Besides, it's not very explicit this empty list is intended as a DataType. And the companion object does not have the associated List methods, so I can't call DataType(), or DataType.empty. It's gonna be even more annoying when I need non-empty lists since I'll have to write out a significant part of this long type.

Is there any way I can ask Scala to understand my type as the same thing, including companion object with its creator methods, in the interest of shortening code and blackboxing it ? Or, any reason why I should not be doing this in the first place ?

like image 777
Jean Avatar asked Oct 13 '10 03:10

Jean


People also ask

What is type alias in Scala?

A type alias is usually used to simplify declaration for complex types, such as parameterized types or function types. Let's explore examples of those aliases and also look at some illegal implementations of type aliases.

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 advantage of companion objects in Scala?

Advantages of Companion Objects in Scala Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class's runtime objects but is available from a static context and vice versa.

Can case class have companion object?

When you want to define some functionality related to a case class you have two possible ways to do this. The first one is to create functions directly in the case class. Note that in order to define a companion object for a class you have to set the same names for them and declare them in the same file.


2 Answers

The answer was actually quite simple:

class ConcreteClass {
  type DataType = List[String]
}
object ConcreteClass {
  val DataType = List
}
val d = ConcreteClass.DataType.empty

This enables my code to call ConcreteClass.DataType to construct lists with all the methods in List and little effort.

Thanks a lot to Oleg for the insight. His answer is also best in case you want not to delegate to List any call to ConcreteClass.DataType, but control precisely what you want to allow callers to do.

like image 164
Jean Avatar answered Sep 26 '22 20:09

Jean


What about this?

class ConcreteClass {
  type DataType = List[String]
}
object DataType {
  def apply(): ConcreteClass#DataType = Nil
}
//...
val a = DataType()

like image 39
Oleg Galako Avatar answered Sep 23 '22 20:09

Oleg Galako