Type classes or implicit parameters? What do you prefer and why? [closed]
- by Petr Pudlák
I was playing a bit with Scalaz and I realized that Haskell's type classes are very similar to Scala's implicit parameters. While Haskell passes the methods defined by a type class using hidden dictionaries, Scala allows a similar thing using implicit parameters.
For example, in Haskell, one could write:
incInside :: (Functor f) => f Int -> f Int
incInside = fmap (+ 1)
and the same function using Scalaz:
import scalaz._;
import Scalaz._;
def incInside[F[_]](x: F[Int])(implicit fn: Functor[F]): F[Int] =
fn.fmap(x, (_:Int) + 1);
I wonder: If you could choose (i.e. your favorite language would offer both), what would you pick - implicits or type classes? And what are your pros/cons?