4 ms·
Maybe a disagreement here is fueled by the power level difference in these two features. Beyond the superficial injection examples, Scala implicits also support
by ionforce 8y ago
Maybe a disagreement here is fueled by the power level difference in these two features. Beyond the superficial injection examples, Scala implicits also support injecting values constructed dynamically and also recursive/derived implicits.
- YorkshireSeason 8y ago> injecting values constructed dynamically You can also do this with default arguments, e.g. like so: let f x y = let g (a = x+1) b = ... ... For example the following is valid OCaml: let bump ?(step = 1) x = let waa ?( a = step+17 ) b = a * b in x + step + waa 666;; Here the function waa has a default argument whose default argument depends on bump's default argument. > recursive/derived implicits Recursion is quite restricted, for example this is not valid Scala: def f ( x : Int ) ( implicit y : Int ) : Int = { implicit val a = 17 f ( x+1 ) }
- lptk 8y agoYour example of nested functions with default arguments does not correspond to what derived implicits do. As a result of implicit resolution, an expression with an arbitrary number of subexpressions may be synthesized, the shape of which depends on the types involves. Default parameters simply cannot do that. The example you give to justify "recursion is quite restricted" is not a restriction on recursion at all, it's an ambiguity problem. Define `a` as `y` to shadow the function parameter, and it compiles.
- YorkshireSeason 8y ago> does not correspond to what derived implicits do I did not claim it did. The first example disproved ionforce's claim that default values cannot be constructed dynamically. > Define `a` as `y` to shadow the function parameter, Whoops, yes that's correct. I didn't realise this was possible.
- lptk 8y ago> The first example disproved ionforce's claim that default values cannot be constructed dynamically. But I don't think that is what they meant. I think they meant something along the lines of what I said above: > an expression with an arbitrary number of subexpressions may be synthesized, the shape of which depends on the types involves
- YorkshireSeason 8y agoOK, maybe I misunderstood ionforce. I don't deny that type-driven synthesis is a core element of implicits.