The magnet pattern is an alternative approach to method overloading.
method overloading in Scala comes with (at least) the following problems and inconveniences:
- "Collisions" caused by type erasure
- No lifting into a function (of all overloads at the same time)
- Unavailability in package objects (before Scala 2.10)
- Code duplication in case of many similar overloads
The magnet pattern can solve these issues
- Magnet
- declare a magnet interface.
- declare a abstract type for result.
- ConcreteMagnet
- implement Magnet interface.
- declare it as implicit class
- Client
- define a function which take a magnet object as argument and return the type of magnet.Result
Overload double
function so that it can process Int, List[Int], List[String] and Tuple2[String, Int]
Participants in this example:
- DoubleMagnet is the Magnet.
- fromInt/fromListInt/fromListString/fromStringIntTuple is the implicit class of ConcreteMagnet.
- Doubling is the Client.
- Magnet pattern is a way of using type-classes pattern for method overloading purpose.