scala - How to (properly) enrich the standard library? -
i define implicit conversion iterator[t]
class have defined: proactiveiterator[a]
.
the question isn't how how properly, i.e. place method, transparent , unobtrusive possible. ideally should implicit conversion string
stringops
in scala.predef
if conversion class in library other class, defined inside class, afaik that's not possible here.
so far have considered add object containing these conversions, javaconversions, better options may possible.
you don't have of choice. implicits must contained within sort of object, , imported wildcard import (you could import them individually, doubt want that).
so you'll have sort of implicits object:
package foo.bar object implicits { implicit class proactiveiterator[a](i: iterator[a]) { ... } }
then must explicitly import wherever use it:
import foo.bar.implicits._
in opinion, thing. reading code might not understand pimped methods coming from, explicit import helpful.
you can place implicits within package object
. have import them same way other namespaces, available classes within same package.
for example, using following, within foo.bar
have implicit class available:
package foo package object bar { implicit class proactiveiterator[a](i: iterator[a]) { ... } }
elsewhere import foo.bar._
(which may or may not clean, depending on what's in bar
).
Comments
Post a Comment