This page is no longer maintained — Please continue to the home page at www.scala-lang.org

type alias at top level ; package

3 replies
Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.

Why is OK to do this:

object Foo { type X = Int }

but not at top (package) level:

type X = Int // error

Related: does a package implicitly define a top-level object?

Also, please let me know if there is a more suitable list for such questions.

Thanks!

marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Re: type alias at top level ; package
You can use package objects for this.
package object Foo {  type X = Int}
Marius

On Mon, Dec 5, 2011 at 6:58 PM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
Why is OK to do this:

 object Foo { type X = Int }

but not at top (package) level:

 type X = Int // error

Related: does a package implicitly define a top-level object?

Also, please let me know if there is a more suitable list for such questions.

Thanks!



odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: type alias at top level ; package

On Mon, Dec 5, 2011 at 8:58 AM, Sophie wrote:
> Why is OK to do this:
>
>  object Foo { type X = Int }
>
> but not at top (package) level:
>
>  type X = Int // error
>
> Related: does a package implicitly define a top-level object?
>
> Also, please let me know if there is a more suitable list for such
> questions.
>
I think this one is most suitable for scala-user. But no harm done
asking it here :-). The answer (use a pcakage object) is correct.

Cheers

Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.
Re: type alias at top level ; package

Thanks, that works.

A current downside is REPL and IDE line noise, as

package object Foo { type X = Int }

will clutter up the REPL (and Scala IDE) with Foo.X instead of X, which
is noise for the intended use.

Relatedly, here is a REPL output:

val capitals = Map("France" -> "Paris")

capitals: scala.collection.immutable.Map[java.lang.String,
java.lang.String] = Map(France -> Paris)

When in the REPL what I'd much prefer:

capitals = Map(France -> Paris)

On 2011-12-05 11:42:29 -0600, martin odersky said:

> On Mon, Dec 5, 2011 at 8:58 AM, Sophie wrote:
>> Why is OK to do this:
>>
>>  object Foo { type X = Int }
>>
>> but not at top (package) level:
>>
>>  type X = Int // error
>>
>> Related: does a package implicitly define a top-level object?
>>
>> Also, please let me know if there is a more suitable list for such
>> questions.
>>
> I think this one is most suitable for scala-user. But no harm done
> asking it here :-). The answer (use a pcakage object) is correct.
>
> Cheers
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland