3
votes

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.

1
Well, have you tried defining it using :paste?Debilski
I cannot find anything on what :paste means :( apparently, it's something to do with REPL, but I want it to work outside of REPL too, of courseKarel Bílek
Just type in the REPL and then copy-and-paste (or type) both the type alias and the val assignment after another. This is a REPL-only problem so it should not matter in other code.Debilski
Oh, OK, I get it, it was just an "issue" of REPL. OK. Thanks!Karel Bílek

1 Answers

5
votes

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.