3 ms·
In layman programmer terms, what are modular implicits? Are they like (please no) Scala's implicits? (Or is there some way Scala's implicits are less scary th
by daxfohl 11y ago
In layman programmer terms, what are modular implicits? Are they like (please no) Scala's implicits? (Or is there some way Scala's implicits are less scary than years past?)
- whateveracct 11y agoScala implicits are really one of the "killer apps" of its type system. No language I know of really has anything quite like it. Scala "implicits" mean two different things: 1) implicit conversions. e.g. If there is an implicit def f(x: Int): String = x.toString in scope, then any time you try to use an Int where a String is expected, Int::toString will be called automatically. Using implicit conversions like this is generally frowned upon and also where the "please no" probably comes from. implicit conversions are used frequently but almost entirely for extension methods (there's even sugar for it now: "implicit class") 2) implicit parameters. These are parameters in a functions type signature keyed by type. You can emulate Haskell typeclasses (sans canonicity) with these. For instance def monoidSum[A](as: List[A])(implicit m: Monoid[A]): A = as.foldLeft(m.zero)(m.append) is equivalent to the Haskell monoidSum :: (Monoid a) => [a] -> a monoidSum as = foldl' mappend mempty Scala implicit parameters can be defined inductively and depend on other implicit parameters. You can do this to perform some really cool type-level computation. The shapeless library is full of stuff like this. Once you understand implicit parameters, it really is somewhat nice to write functions that operate on arbitrarily-sized HLists and Coproducts. Modular implicits are similar to (and inspired by?) Scala implicit parameters.
- ubertaco 11y agoIf you don't mind reading a paper, there's a paper on it that actually is readable instead of just all whitepapery and overly-theoretical: http://arxiv.org/pdf/1512.01895.pdf http://arxiv.org/pdf/1512.01895.pdf