Pregunta

I want to make my Scala code more readable, so I added custom types for all parametrized types.

So I have in package object, for simplicity,

type IntSeq = Seq[Int]

However, now I cannot do simple apply on companion object. From REPL:

scala> IntSeq(1, 2, 3)
<console>:8: error: not found: value IntSeq
              IntSeq(1, 2, 3)
              ^

What to do?

(just to make sure: my actual aliased objects are more complicated than Seq[Int])

edit: There is a similar question - Scala type alias including companion object [beginner]

On that question, there are two replies, both of them not working.

One is to define my custom object with apply, but I am not sure how to do that in my case, plus it is a little verbose.

The other - to write val IntSeq = Seq produces the error

warning: previously defined trait Seq is not a companion to value IntSeq. Companions must be defined together; you may wish to use :paste mode for this.

¿Fue útil?

Solución

The second error is just because of the way REPL operates. In REPL, the companions must be defined together using the :paste mode; however, in the package object, that is not an issue.

So, the other approach - to write val IntSeq = Seq - will actually work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top