3

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.

Community
  • 1
  • 1
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
  • 1
    Well, have you tried defining it using `:paste`? – Debilski Jul 08 '12 at 01:31
  • 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 course – Karel Bílek Jul 08 '12 at 01:32
  • 1
    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 Jul 08 '12 at 01:33
  • Oh, OK, I get it, it was just an "issue" of REPL. OK. Thanks! – Karel Bílek Jul 08 '12 at 01:34

1 Answers1

5

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.

Karel Bílek
  • 36,467
  • 31
  • 94
  • 149