I defined implicit conversions in an object
. Let's call the object Implicits
and there is one implicit conversion in it.
package com.gmail.naetmul.stackoverflow.app
object Implicits {
implicit def int_to_intEx(x: Int): IntEx = IntEx(x)
}
This object is in some package. I want to use this implicit conversion in every code in the package com.gmail.naetmul.stackoverflow.app
and all of its sub-packages like com.gmail.naetmul.stackoverflow.app.something.anything.everything
.
So I made the package object of com.gmail.naetmul.stackoverflow.app
.
package com.gmail.naetmul.stackoverflow
package object app {
import com.gmail.naetmul.stackoverflow.app.Implicits._
}
But it didn't work outside of the exact package object.
So I changed the object Implicits
to trait
and let the package object extend it.
package com.gmail.naetmul.stackoverflow.app
trait Implicits {
implicit def int_to_intEx(x: Int): IntEx = IntEx(x)
}
package com.gmail.naetmul.stackoverflow
import com.gmail.naetmul.stackoverflow.app.Implicits
package object app extends Implicits {
// some code
}
The implicit conversion worked in the package com.gmail.naetmul.stackoverflow.app
. However, it either worked or did not work in the sub-package.
For example)
File A.scala
package com.gmail.naetmul.stackoverflow.app.database
class A {
// Here, the implicit conversion did NOT work.
}
File B.scala
package com.gmail.naetmul.stackoverflow.app
package database
class B {
// Here, the implicit conversion DID work.
}
So the question is:
Should I use
trait
instead ofobject
in this case (using with the package object, but defined outside)?Is there another way to use the implicit conversions in subpackages? I mean, import only once, and use them everywhere. The way that worked in
B.scala
seems fine, but the Eclipse's default package statement is likeA.scala
, so I have to change them manually.