4 ms·
Is "Pimp my Library", as used to be recommended by Odersky, also an anti-pattern now? It uses implicit conversions, if I'm not mistaken.
by the_af 11y ago
Is "Pimp my Library", as used to be recommended by Odersky, also an anti-pattern now? It uses implicit conversions, if I'm not mistaken.
- dudul 11y agoIt does, and yes I think it is now considered an anti-pattern. The proper way (IMO) to extend libraries is to use type-classes.
- sjrd 11y agoNo, Pimp my Library is not an anti-pattern. It even received dedicated syntax with `implicit class`es. Contrary to general implicit conversions, implicit classes are not dangerous because things are not actually converted as far as the developer can see (yes, under the hood, it's an implicit conversion, but it doesn't bite you). You're simply adding methods on existing classes, which is similar to extension methods in several other languages.
- wtetzner 11y agoAnd in fact, if you combine implicit classes with values classes (e.g. implicit class RichFoo(val value: Foo) extends AnyVal { ... }) then you're effectively just adding extension methods, and there's not even an allocation involved.
- the_af 11y agoI didn't even know implicit classes existed. Thanks!