Wildcard Import, then Hide Particular Implicit?

2019-07-12 11:28发布

问题:

Given the following object with 2 implicits:

scala> object Foo {
     |  implicit def stringToInt(x: String) = 555
     |  implicit def stringToBoolean(x: String) = true
     | }
warning: there were two feature warnings; re-run with -feature for details
defined object Foo

I can use them:

scala> def f(x: Int) = x
f: (x: Int)Int

scala> import Foo._
import Foo._

scala> f("asdf")
res0: Int = 555

scala> def g(b: Boolean) = b
g: (b: Boolean)Boolean

scala> g("asdfasdf")
res1: Boolean = true

Then, I tried to disable the stringToInt implicit.

scala> import Foo.{stringToInt => _}
import Foo.{stringToInt=>_}

But, evidently, that did not work.

scala> f("adsfasdf")
res2: Int = 555

After wildcard importing implicits, is it possible to hide them?

Basically, I'd like to use all of Foo's implicits, minus a single one, stringToInt.

Note - of course I could simply do import Foo.stringToBoolean only, but, for my scenario, Foo has ~25 imports, and I want to use 24 of them. As a result, it's more concise to use all, and then subtract one.

回答1:

The REPL just approximates what to import from history, and one approximation is always to use imported implicits.

In normal code, you would disable an implicit by shadowing the identifier.

Best shot is:

scala> object X { val stringToInt = 0 }
defined object X

scala> import X._
import X._

scala> f("asdf")
<console>:20: error: type mismatch;
 found   : String("asdf")
 required: Int
       f("asdf")
         ^

or

scala> implicit val stringToInt = 0
stringToInt: Int = 0

The idea is to introduce the shadowing variable into the scope of the REPL template for the current line.



回答2:

There is an existing syntax to disallow a specific import. It is an extension of the syntax for renaming/aliasing an import:

import scala.collection.concurrent.{ Map => CMap } // Import concurrent Map, but alias it to "CMap", so the predef-imported "Map" isn't shadowed.

To hide an import, you can alias it to "_":

import scala.collection.mutable.{ Map, TrieMap => _ } // Import Map, but not TrieMap

Similarly, to import all of an object, except for a specific entry, use:

import Foo.{ stringToInt => _, _ }

scala> def g(b: Boolean) = b
g: (b: Boolean)Boolean

scala> g("asdfasdf")
res1: Boolean = true

scala> def f(x: Int) = x
f: (x: Int)Int

scala> f("asdf")
<console>:13: error: type mismatch;
 found   : String("asdf")
 required: Int
              f("asdf")
                ^