135 points · by maddening · 8y ago · 71 comments
(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 :-\
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.
Usually the rules like these, LOC written, bugs closed per month tend to promote gamification around them.
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.
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.
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.
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/splainA search tree is cool, but it's not going to explain how to get a value when it has restrictions (dependencies).
It is utterly essential if you are doing anything with implicits. In particular with libraries like Circe.
> 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
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...
What it is is highly front-loaded, especially through the early learning cliff of integrating the borrow checker.
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...