how to import commonly-used classes/implicits easily?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to import commonly-used classes/implicits easily?

brian-245
How should I arrange for the classes and implicits I tend to use
everywhere to be imported easily? I'm getting the job done now with
several imports, but it would be nice if I could import Usual._ and
have everything. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: how to import commonly-used classes/implicits easily?

Jorge Ortiz
I'm afraid there's no way to do this at the moment.

If the export proposal (http://scala.sygneca.com/future/export) ever
becomes a reality, this would be one of its many uses.

--j

On Tue, Jul 22, 2008 at 5:46 PM, brian <[hidden email]> wrote:
> How should I arrange for the classes and implicits I tend to use
> everywhere to be imported easily? I'm getting the job done now with
> several imports, but it would be nice if I could import Usual._ and
> have everything. Thanks.
>
Reply | Threaded
Open this post in threaded view
|

Re: how to import commonly-used classes/implicits easily?

Aaron Harnly-2-2
In reply to this post by brian-245
brian <brianchina60221@...> writes:
> How should I arrange for the classes and implicits I tend to use
> everywhere to be imported easily? I'm getting the job done now with
> several imports, but it would be nice if I could import Usual._ and
> have everything. Thanks.

If you've written the modules that you want to import, there is a
technique that works pretty well. David MacIver suggested it
half-jokingly on IRC, but I've been using it for a while now and find it
very useful.

Supposing that now you have three different modules, in three different
files (or packages) that declare implicit conversions (or utility
methods, or whatever):

object FooImplicits {
  implicit def string2foo...
}

object BarImplicits {
  implicit def int2bar...
}

object BazImplicits {
  implicit def symbol2baz...
}

Refactor those into traits, with an accompanying object that implements
the trait: trait FooImplicits { implicit def string2foo... } object
FooImplicits extends FooImplicits

trait BarImplicits {
  implicit def int2bar...
}
object BarImplicits extends BarImplicits

trait BazImplicits {
  implicit def symbol2baz...
}
object BazImplicits extends BazImplicits

Now, if you usually import all three of those together, just create an
object combining them:

object UsualImplicits
extends FooImplicits
with BarImplicits
with BazImplicits

And now you can just import UsualImplicits._ in your client code. A
slightly improved version is to also create UsualImplicits as both a
trait and an object. This also allows you to create several variants on
your "standard" environment:

trait UsualImplicits
extends FooImplicits
with BarImplicits
with BazImplicits

object UsualImplicits extends UsualImplicits

trait UsualWithColorImplicits
extends UsualImplicits
with ColorImplicits

object UsualWithColorImplicits extends UsualWithColorImplicits

etc.

Hope that helps,
aaron