5
votes

I think this is easier to show with an example.

Let's say I have a Condition case class, with a Condition companion object used to provide an alternative constructor, like this:

case class Condition(
  field: String, 
  values: List[String])
}

object Condition {
  def apply(field: String, value: String): Condition = {
    Condition(field, List(value))
  }
}

When I import it from another I get the following warning (which eventually turns into an error):

import utils.query.Condition 

[warn] [...]/ConditionBuilder.scala:14: imported `Condition' is permanently hidden by definition of object Condition in package query
[warn] import utils.query.Condition
[warn]        ^
[warn] one warning found

I want to have access to the Condition Type, when declargin the type of a variable, and to the companion object, when executing one of it's methods

Is there some way to achieve this and avoid this warning (other than renaming the companion object, of course)?

3
Which version of the Scala compiler are you using?Otavio Macedo
May I ask if Condition is defined in package query by any chance? In this case, just drop the import and you're done.Régis Jean-Gilles
I'm using the scala compiler that comes bundles with play 2.0.3: Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_06).opensas
Yeap, Condition class, and the Condition companion object, are both defined in package utils.query, and even in the same file... Have a look at the workaround I've found...opensas
Sorry, I should have asked "is your import statement in package query too". Because if you try to import a class/object that is in already in the current package where you do the import (and which is thus allready implicitly accessible), you do get the error that you posted. The proper solution then is to simply remove the import, this will still compile, and you have no more warning. If that is not the case here, maybe post a more complete code snippet so that we can better help.Régis Jean-Gilles

3 Answers

2
votes

so far now, the workaround I found is importing like this:

import utils.query

[...]

val myCondition: query.Condition

and the warning is gone, but I think there should be a better solution...

1
votes

What you describe isn't the way it works (or provide a complete sample).

It sounds like a stale compilation environment. Were package objects involved at any point? Do a clean compile, restart whatever resident compiler, and call us in the morning.

For instance, an import clause import p.{x => y } renames the termname p.x to the termname y and the type name p.x to the type name y. (SLS 4.7)

But there's a big comment in the code around the intersection of case classes and their companions, which includes this top-ten fun comment:

 // What exactly this implies and whether this is a sensible way to
 // enforce it, I don't know.

I'm not saying the comment has any necessary bearing on your case, but it's a great line.

0
votes

I had a main class with name Server and I was creating a jetty server in the main class in the following way:

     import org.eclipse.jetty.server.Server

     var server:Server=new Server()

I got the below warn on running sbt run

    [warn] /home/xxx/xxx/xxx/src/main/scala/com/xxx/xxx/main/Server.scala:3: imported `Server' is permanently hidden by definition of object Server in package main
    [warn] import org.eclipse.jetty.server.Server
    [warn]                                 ^
    [warn] one warning found

I renamed my main class and the warning disappeared.