5 ms·
I've seen uses of implicits fall into three categories: (1) Dependency injection, e.g. ExecutionContext (2) DSLs/syntax sugar, e.g. custom string interpolatio
by paulddraper 8y ago
I've seen uses of implicits fall into three categories:
(1) Dependency injection, e.g. ExecutionContext
(2) DSLs/syntax sugar, e.g. custom string interpolation, specs2
(3) Typeclasses and type-level programming, e.g. Play JSON Reads/Writes, cats, shapeless
The more I have seen Scala, the more I realize that #1 is cancer.
Implicits should not be used to reduce the number of characters typed. If used at all, they should be used for a better reason, e.g. it is a necessary part of a typesafe pattern, like typeclasses.
- bad_user 8y agoImplicits are great. People unfortunately go to great length to avoid typing function parameters so will do all kinds of shit to reduce arguments in function calls. This includes all kinds of insane dependency injection frameworks that depend on runtime introspection or bytecode manipulation, or stateful objects being passed around instead of plain values. There's even this best practice around that says "have functions with 3 params or less" which is complete bullshit. I mean, you actually have company wide policies with enforced lint rules on maximum number of params and people end up passing around giant records (maps, objects) that then get unwrapped. I cannot blame implicits, if anything, if people are going to go to such great lengths to avoid passing arguments in functions, I'd rather see implicits instead of Spring or Guice, because at the very least implicits are compile time. The actual cancer are the best practices that drive people to doing this.
- forkerenok 8y agoI wouldn't say that best practices drive people to doing stupid stuff, at least not directly. And they usually do have merit. The issue is that people apply them without understanding the motivations behind. F.x. the "too many method or constructor args" warning signals that a class or a method is probably doing too much, so please decompose. A person without understanding the motivation will think it's a sort of a game and wrap some params into a map or move some them into globals :-\
- pjmlp 8y agoThat is human psychology at work. Usually the rules like these, LOC written, bugs closed per month tend to promote gamification around them.
- piva00 8y agoExactly, I always remember Goodhart's Law [0] whenever I see metrics popping up as goals, be it for OKRs or as guidelines for coding. [0] https://en.wikipedia.org/wiki/Goodhart%27s_law https://en.wikipedia.org/wiki/Goodhart%27s_law
- mlthoughts2018 8y agoI find it insane that someone would use an actual lint rule to enforce a best practice about few function parameters (loosely defended by this thin complexity argument). Functions of appropriate complexity routinely have more than 3 arguments, even in standard library functions. Why not just use common sense and code review? If a certain function actually needs 10 arguments for customization (for example, this is common in plotting libraries), so be it. If during code review the writer can give a reasonable account of why the complexity is useful, then just move on. I cannot see any way to defend putting this is a linting rule.
- forkerenok 8y agoI'm not a fan of those either (and 3 arguments is insanely low indeed), but I see a way it can work: you always have a choice to suppress a linter finding if your case is "legitimate". F.x. in our case the limit is 7. I think most people would agree that 7+ args constructor or method is worrying. But there are cases where it can be ok (with or without some stretch), f.x. if it's some sort of a glue code that does a very mechanical thing and breaking it down would be nothing short of gold-plating. Edit: In general I find the signaling that linter gives useful. At the same time I agree that mature teams can do without it just fine.
- couchand 8y ago
- ljm 8y agoI've felt similar after seeing the rise of Rubocop after Sandi Metz did some talks and wrote a book about object oriented design in Ruby. Her advice is totally sound, if you hear her out she does not advocate for the dogmatism that had since infected the community (and the linting tools...). Yet still, you will work for a company who insists on a hard and fast rule that a method cannot be more than 10 lines long. Or a class cannot contain over 100 lines. It's not on the same level as implicits and everything else Scala lets you do, but still...it makes code worse. Your 11 line long function that is perfectly readable has to become a shorter function that calls other methods that wrap the same logic, but are otherwise not useful in any other context. Or you have to fuck with the syntax to reduce the line count. You end up with code full of indirection because the community decided to quantify readable code and build policy around it. I find it infuriating.
- wtetzner 8y agoYeah, and the fact that Scala has named parameters means that call sites with a bunch of parameters isn't the problem it is in a language like Java.
- paulddraper 8y agoAnd even if you really do want to have "X params or less", Scala makes it really easy to achieve with case classes. Group your parameters into data classes of X parameters or less, and viola, you have reasonably succinct, readable solution.
- jjtheblunt 8y agoalso agree that best practices are presumptious nonsense.
- foolfoolz 8y agoeh it’s not that clear. all uses of implicits reduce the number of characters typed. there’s no functionality they unlock that you would not gain with another explicit parameter (or 2) and a helper class/type(s). by reducing characters typed they enable a different syntax that you wouldn’t normally do because without implicits it’s cumbersome. but then if code is unwieldy because it’s got too many letters, there’s probably a good argument for a feature that reduces letters. i agree about ExecutionContext. I have done a lot of scala and really find implicits useful to reduce lines but the cost is so high. it’s to point where the scala chat room at work is people posting compiler messages and the answer a lot of the time is you need this magic import. the takeaway i have is if functional programming and monads and referential transparency are some top tier of programming, is this import Implicits._; MoreImplicits._ and param’s of A[B[_], D] really the way things should be? is this what people as eschewing when they say functional is the way to go? it just feels so overkill but you have to use it all over your stack to make the map/flatmap chain work. and once every method is just some long for comprehension you are finally “winning”
- nine_k 8y agoIn Haskell, typeclasses are language-level construct, not needing any implicits, and monadic code is easy to write using do-notation which is syntax sugar over binding ("comprehensions"). In Scala, typeclasses are a pattern, something achievable using a bunch of language features strung together, and implicits are likely an inevitable part of that mix. For-comprehensions is what "sequential computation" really is if you want to represent it as a function, and not forget to check intermediate results. It has numerous advantages, but again, on the language level it's a pattern, a contraption that makes monadic style possible, but not necessarily easy on the eyes.
- paulddraper 8y ago> all uses of implicits reduce the number of characters typed You're technically correct, the best kind of correct. There are some magic mechanisms that do become more useful than they are obtuse. For example, OOP's very widely used polymorphism. I agree that the wildcard import mechanism can be really inscrutable.
- azernik 8y agoA fourth is monkey-patching (if you're counting implicit classes, not just implicit arguments to methods).
- paulddraper 8y agoIMO, that falls in DSLs/syntax sugar. But yeah, for most code I'm not a real fan of most "extension methods." Is 5.minutes really superior to minutes(5) or duration(5, minutes)?
- mpartel 8y agoIn your example, not really, but in general, assuming the implicit is in scope most of the time, `x.method` is much more discoverable through autocomplete than `method(x)`. Incidentally, the fact that Haskell's function calls look like `f x` instead of `x.f` probably makes building a pleasant Haskell IDE quite difficult.
- pjmlp 8y agoLeksah is quite nice.
- paulddraper 8y ago> `x.method` is much more discoverable With an IDE, yes. With a text editor, no. > assuming the implicit is in scope most of the time, And that's really the rub. I write import com.example.MagicThings._ and one of the things imported is an implicit class or method which has the visible on that. The name I see in the code is not even one of the names I am importing, which IMO significantly hurts discoverability.
- mpartel 8y agoWithout an IDE, discoverability is not great even in implicitless code. In `x.y.z()`, you don't need to import the type of `y`, much less the method `z`. And an IDE's "go to definition" works fine on extension methods. IMO the greater discoverability problem is knowing and remembering what to import in the first place. Still, even with an IDE, extension methods do add complexity and should only be considered when they'd be called very frequently. In my current (Unity/C#) codebase, the ratio of static helper methods to extension methods is probably around 10:1 or more, and that seems alright. Although it sounds very controversial, it might actually be good if libraries could by default add their implicits to the prelude. SBT (which is horrible in many other ways) lets its plugins do something similar, and that seems to be ok.
- deleted 8y ago[deleted]
- acjohnson55 8y agoTo each their own, but I think #1 is great. It allows you to separate the plumbing of your system from the logical parts in your APIs.
- rockostrich 8y agoMy team decided to avoid #1 at all costs recently. The prime example was for Twitter's Timer class which for some reason is an implicit argument in a lot of their library code. We just always pass in timers explicitly now. We generally only use implicits for (de)serialization, whether it be JSON, bytes, encoded strings, etc. The one exception is our DAG state machine code which uses implicits to resolve which data is available at each step. I didn't work on the base code, but it makes writing DAGs really straight forward and mostly painless. Also for anyone that sees this, Intellij has a shortcut for viewing which implicits are being used in statements (CMD + Shift + P on MacOS). It's really helpful if you're trying to figure out implicit resolution.
- mac01021 8y agoWhich of those categories contains CanBuildFrom? It's #1, right? But instead of the number of characters typed, I think its primary value is in removing (from the call site) implementation details that the reader of the code need not be concerned with. Maybe I'm wrong. Maybe it's cancer.
- jjtheblunt 8y agototally agree that (1) is ridiculously gratuitous nerdiness.