Scala: prevent companion object from hiding source class definition

I think this is easier to show with an example.

Say I have a case class of conditions, with an accompanying condition object that is used to provide an alternative constructor, for example:

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 type of the condition when declargin is the type of the variable and the companion object, when one of its methods is executed

Is there a way to achieve this and avoid this warning (except, of course, renaming the companion object)?

+5
source share
3 answers

So far, the workaround I found is imported as follows:

import utils.query

[...]

val myCondition: query.Condition

, , ...

+2

, , - , ( ).

. - ? , .

, import import p. {x = > y} p.x y p.x y. (SLS 4.7)

case , - :

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

, - , .

+1

Server, :

     import org.eclipse.jetty.server.Server

     var server:Server=new Server()

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

, .

0
source

All Articles