Does it matter if I import implicits more than once as part of trait?

Example:

trait Implicits {
  implicit def myImplicit .......
}

trait MyFoo extends Implicits {
}

trait MyBar extends Implicits {
  val myFoo = new MyFoo {

  }
}

Is there an implicit ambiguity inside myFoo? Are there two duplicates of the implicit in scope?

I think that there is no ambiguity. Scoping rules apply. Try the following code

trait Monoid[M] {

  val mUnit: M

  val mAppend: (M, M) => M

}


trait Implicits {

  implicit val intMonoid = new Monoid[Int] {

    override val mUnit: Int = 0

    override val mAppend: (Int, Int) => Int = _ + _

  }

}

trait MyFoo extends Implicits {

  import intMonoid._

  println(s"in MyFoo ${mAppend(mUnit, mUnit)}")

}

trait MyBar extends Implicits {

  override implicit val intMonoid = new Monoid[Int] {

    override val mUnit: Int = 1

    override val mAppend: (Int, Int) => Int = _ * _
  
  }  

  import intMonoid._

  println(s"in MyBar ${mAppend(mUnit, mUnit)}")

  val myFoo = new MyFoo {

    import intMonoid._

    println(s"in myFoo ${mAppend(mUnit, mUnit)}")

  }  

}

object ImplicitsApp extends App {

  val myBarObject = new MyBar {

    import intMonoid._

    println(s"in myBar ${mAppend(mUnit, mUnit)}")

  }

}
$ fsc Implicits.scala 
$ scala ImplicitsApp
in MyBar 1
in MyFoo 0
in myFoo 0
in myBar 1

and with the overridding implicit val commented out

$ fsc Implicits.scala 
$ scala ImplicitsApp
in MyBar 0
in MyFoo 0
in myFoo 0
in myBar 0

I think that in Scala 2 normal scoping rules don’t apply to implicits. This is changed in dotty. However myImplicit in MyFoo will shadow myImplicit in MyBar because it has the same name, and in that case there is no ambiguity. In a different scenario where both implicits would have different names, you would have a problem.

scala> trait Implicits { implicit val strImpl = "foo" }
defined trait Implicits

scala> trait Foo extends Implicits
defined trait Foo

scala> trait Bar extends Implicits { val foo = new Foo { implicitly[String] } }
defined trait Bar

scala> trait Baz extends Implicits { implicit val a = 1; val foo = new Foo { implicit val b = 2;  println(implicitly[Int]) } }
<console>:13: error: ambiguous implicit values:
 both value a in trait Baz of type => Int
 and value b of type => Int
 match expected type Int
       trait Baz extends Implicits { implicit val a = 1; val foo = new Foo { implicit val b = 2;  println(implicitly[Int]) } }
                                                                                                                    ^

OK, thanks. Should it affect performance? In other words, if the implicits are introduced only once in the scope is it better or is the shadowing effect insignificant?

I don’t know enough about the implementation to have a definitive answer. But I suspect it doesn’t really affect performance. I guess the compiler just looks at all names that are in the current scope (as part of the first phase of implicit search) and filters out the ones that are implicit and have a compatible type. If a name is shadowed then it is simply no longer in scope, and it will not be found.