stacycurl / pimpathon

Adds useful methods to scala & java classes.
Apache License 2.0
35 stars 9 forks source link

a word of warning on any attempts to do a RichTraversable[(A, B)] #166

Closed fommil closed 9 years ago

fommil commented 9 years ago

recall that scala has an implicit conversion from everything to String. I am aware of an implementation in a codebase of a certain RichTraversable which has a

  def get(key : A) : Option[B] = traversable.filter(_._1 == key).headOption.map(_._2)

unfortunately, this means that all maps get implicitly converted into RichTraversables and calling .get("with a string") will happily compile.

A good lesson in why pimped methods should have a different name to canonical implementations.

stacycurl commented 9 years ago

Hmm, if I have

val m = Map(1 -> 2.0, 2 -> 3.0) val t: Traversable[(Int, Double)] = m

implicit class RichTraversable2[A, B](t: Traversable[%28A, B%29]) { def get(key: A): Option[B] = None }

then neither of the following lines compiles: m.get("a string") t.get("a string")

Pimpathon does have at least one 'overloaded pimp': MapOps[K, V].get(ok: Option[K]): Option[V]. What do you think of that one ?

On 10 November 2014 17:02, Sam Halliday notifications@github.com wrote:

recall that scala has an implicit conversion from everything to String. I am aware of an implementation in a codebase of a certain RichTraversable which has a

def get(key : A) : Option[B] = traversable.filter(_.1 == key).headOption.map(._2)

unfortunately, this means that all maps get implicitly converted into RichTraversables and calling .get("with a string") will happily compile.

A good lesson in why pimped methods should have a different name to canonical implementations.

— Reply to this email directly or view it on GitHub https://github.com/stacycurl/pimpathon/issues/166.

Stacy

fommil commented 9 years ago

there must be some other magic helping out