Scala: Understanding implicits
kubuszok.com
kubuszok.com
(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.
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.
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 :-\
Usually the rules like these, LOC written, bugs closed per month tend to promote gamification around them.
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.
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.
It's indeed "e.g." as a sibling comment suggests.
The point of directives that suppress specific linter rules is that it should be used in rare, exceptional cases when an otherwise common lint rule needs to be violated.
But having a large number of function parameters is not rare or exceptional: you have that for legitimate reasons all the time.
If a lint rule causes lint overrides to become a daily experience, I’d say that clearly tells you that the lint rule itself is wrong, not the software practices.
To me, someone who would support a lint rule reducing function parameters would almost certainly be someone who had a deeply unpragmatic and unrealistic obsession with certain kinds of design patterns and refactoring patterns.
You code professionally long enough and you start to realize that most design patterns, especially OO cookie cutter patterns, are pretty crap and only have a few narrow use cases.
Introducing some type of abstraction purely to allow refactoring into function signatures with fewer parameters would be a nasty bad code smell to me.
I think here we can only speak of our own experiences. In my team's experience that linter rule had been triggered only 2-3 times in 2 year history. Far from being "daily experience". And in all cases it was warranted: it was a poor choice of abstraction that resulted in accumulation ever-growing number of dependencies in a class.
> Introducing some type of abstraction purely to allow refactoring into function signatures with fewer parameters would be a nasty bad code smell to me.
Referring to my earlier comment, the point is not to blindly refactor something to satisfy the linter, but to actually understand:
* is there really a problem (in _your_ terms) behind a given linter signal?
* if there is a problem - fix it
* if not - well, suppress it for this instance, adjust or get rid of this linting rule.
> You code professionally long enough and you start to realize that most design patterns, especially OO cookie cutter patterns, are pretty crap and only have a few narrow use cases.
When it comes to OO patterns I tend to agree: many are quite narrow and the way they are pitched ends with people looking for nails with a bunch of hammers. I.e. people keep "applying patterns" instead of fixing problems in their code. But those kind of linting rules are not about design patterns.
Edit: formatting.
That doesn't match my experience. IME, virtually most cases where I've run into asignaturas with more than about 3 parameters, there have been one or more groups that should have been passed to another function whose result should have been passed to the present function.
But that may be context dependent.
> If a lint rule causes lint overrides to become a daily experience, I’d say that clearly tells you that the lint rule itself is wrong
Sure, so in a context where you need to do that daily, the lint rule is bad for that context.
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.
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”
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.
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.
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)?
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.
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.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.
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.
Maybe I'm wrong. Maybe it's cancer.
`-Xlog-implicits` is good to use every now and then, but it quickly becomes unreadable for any decent sized project.
[0]: https://github.com/tek/splainIt is utterly essential if you are doing anything with implicits. In particular with libraries like Circe.
A search tree is cool, but it's not going to explain how to get a value when it has restrictions (dependencies).
> why bother
Because software development is hard, we're solving hard problems and it's good to have better and better tools to do that.
In particular Scala's implicit parameters, along with its support for higher kinded types and functional programming in general allow for modeling your problem domain via tools imported from Haskell and other FP languages, because implicit parameters are in fact a generalization of type classes. And type classes are many times a superior alternative to OOP for building polymorphic code.
Plus the landscape is very competitive and the market is global. I remember in 2000 when the bubble burst the jobs of thousands of developers disappeared over night. To stay competitive, a "why bother" attitude is not going to cut it when the current bubble bursts, just saying.
That seems like too limiting a definition of "magic" to me.
> implicit parameters are in fact a generalization of type classes.
Well, not exactly, as far as I can tell. Type classes (in Haskell at least) are significantly more ergonomic. Since they semantically describe the user's types, they allow you to perform their operations directly with the user's values. Implicit parameters have to be pulled out of the implicit environment and used explicitly themselves, as far as I can tell.
It's true that the underlying mechanism is (roughly) the same, but the surface-level behavior is what really matters, and there they are very different.
What is your definition of "magic" then? Things that are unfamiliar?
Please don't mention "explicit vs implicit", because Scala's implicits are in fact explicit, plus we can always talk of assembly language and how it lacks any magic.
> "Well, not exactly, as far as I can tell. Type classes (in Haskell at least) are significantly more ergonomic."
Ergonomics has nothing to do with whether a concept is a generalization of another.
> "Since they semantically describe the user's types, they allow you to perform their operations directly with the user's values. Implicit parameters have to be pulled out of the implicit environment and used explicitly themselves, as far as I can tell."
Nope, both type clases and implicit parameters allow for describing functions that turn type names into values. That's all there is to it. Or in other words, both type classes and Scala's implicits are about return type polymorphism.
The only actual differences are that:
1. type classes in Haskell are supposedly coherent, i.e. the compiler is supposed to force a single type class instance in the whole project, however GHC does in fact not do that so you can end up with multiple instances of the same type in the same project
2. Scala's implicits are more flexible in what they allow, for example it has priority rules in case of conflicts, which allow say those implicits to work in the presence of OOP subtyping, or to work with multiple type params for describing things that are difficult to describe via type classes (but not necessarily desirable, e.g. the CanBuildFrom pattern)
3. working with Haskell's type classes is nicer, because for hierarchies in Scala you end up dealing with OOP issues (i.e. in modelling Haskell's type class hierarchies, we ended up debating on inheritance vs composition), plus we do a lot of manual plumbing in libraries like Typelevel Cats
But trust me when I say this, fundamentally they are the same ;-)
You can also read Martin Odersky's paper about it: http://ropas.snu.ac.kr/%7Ebruno/papers/TypeClasses.pdf
Can you give an example?
According to [1] a working version will be ready before originally planned:
> Recently, Stu Hood came up with a groundbreaking idea. He suggested > that Rsc can become useful much earlier that we initially thought.
[0] https://github.com/twitter/rsc
[1] https://github.com/twitter/rsc/commit/ebc3019c06c9e175311541...
Diagnostics:
- A method takes an implicit parameter to be filled in by the compiler
- the Scala compiler synthesizes an argument for that parameter using macros
- instead of using an existing value in the implicit scope
Conclusion: that's why a small source file took so long to typecheck.
Details at https://jobs.zalando.com/tech/blog/achieving-3.2x-faster-sca...
Once upon a time I wrote a toy Android app with Scaloid, and tried to use some on-device print-debugging. toast("foo") worked just fine. toast(42) crashed at runtime. Turns out Scaloid offers an implicit conversion from Int to CharSequence, and it even doesn't turn 42 into anything like "42". I'm not kidding [1]. In Android you normally have "resource identifiers" as integers with no compile-time type information, and then at runtime you never use these as actual numbers, instead you every single time pass them to some function like getText or getDrawable. Sound like the perfect case for implicit conversion, right?
[1] https://github.com/pocorall/scaloid/blob/b4fcbd0c25870941af7...
What it is is highly front-loaded, especially through the early learning cliff of integrating the borrow checker.