TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Scala: Understanding implicits

135 pointsby maddeningalmost 7 years ago

14 comments

paulddraperalmost 7 years ago
I&#x27;ve seen uses of implicits fall into three categories:<p>(1) Dependency injection, e.g. ExecutionContext<p>(2) DSLs&#x2F;syntax sugar, e.g. custom string interpolation, specs2<p>(3) Typeclasses and type-level programming, e.g. Play JSON Reads&#x2F;Writes, cats, shapeless<p>The more I have seen Scala, the more I realize that #1 is cancer.<p>Implicits should <i>not</i> 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.
评论 #17590857 未加载
评论 #17590882 未加载
评论 #17592568 未加载
评论 #17590943 未加载
评论 #17596334 未加载
评论 #17592513 未加载
评论 #17591032 未加载
评论 #17596420 未加载
harpocratesalmost 7 years ago
I heartily recommend [splain][0] to anyone debugging non-trivial implicits. It is a scalac compiler plugin that, among other things, will swap out the horribly unhelpful &quot;implicit not found&quot; or &quot;diverging implicit expansion&quot; messages for an indented and annotated representation of the search tree that the compiler went through before giving up.<p>`-Xlog-implicits` is good to use every now and then, but it quickly becomes unreadable for any decent sized project.<p><pre><code> [0]: https:&#x2F;&#x2F;github.com&#x2F;tek&#x2F;splain</code></pre>
评论 #17590865 未加载
评论 #17590809 未加载
评论 #17590849 未加载
weegoalmost 7 years ago
The best summary is why bother when coding explicitly and without &quot;magic&quot; is far more suitable to long term maintainable code
评论 #17590932 未加载
评论 #17590868 未加载
评论 #17595078 未加载
评论 #17596450 未加载
AheadOfTime295almost 7 years ago
It will be interesting to try out implicits in the upcoming Twitter&#x27;s Reasonable Scala Compiler [0]<p>According to [1] a working version will be ready before originally planned:<p>&gt; Recently, Stu Hood came up with a groundbreaking idea. He suggested &gt; that Rsc can become useful much earlier that we initially thought.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;twitter&#x2F;rsc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;twitter&#x2F;rsc</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;twitter&#x2F;rsc&#x2F;commit&#x2F;ebc3019c06c9e175311541637ec31047fb914c4b" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;twitter&#x2F;rsc&#x2F;commit&#x2F;ebc3019c06c9e175311541...</a>
评论 #17591062 未加载
justinjlynnalmost 7 years ago
For when you&#x27;ve completed that here&#x27;s an application (with an amusing name): <a href="http:&#x2F;&#x2F;eugenezhulenev.com&#x2F;blog&#x2F;2017&#x2F;04&#x2F;26&#x2F;type-level-instant-insanity-in-scala&#x2F;" rel="nofollow">http:&#x2F;&#x2F;eugenezhulenev.com&#x2F;blog&#x2F;2017&#x2F;04&#x2F;26&#x2F;type-level-instant...</a>
评论 #17590701 未加载
AheadOfTime295almost 7 years ago
Re implicits and their interplay with macros: Achieving 3.2x Faster Scala Compile Time<p>Diagnostics:<p>- A method takes an implicit parameter to be filled in by the compiler<p>- the Scala compiler synthesizes an argument for that parameter using macros<p>- instead of using an existing value in the implicit scope<p>Conclusion: that&#x27;s why a small source file took so long to typecheck.<p>Details at <a href="https:&#x2F;&#x2F;jobs.zalando.com&#x2F;tech&#x2F;blog&#x2F;achieving-3.2x-faster-scala-compile-time&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jobs.zalando.com&#x2F;tech&#x2F;blog&#x2F;achieving-3.2x-faster-sca...</a>
joshlemeralmost 7 years ago
Also see Martin Odersky&#x27;s recent talk about abstracting context using implicits <a href="https:&#x2F;&#x2F;youtu.be&#x2F;uiorT754IwA" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;uiorT754IwA</a>
cutleralmost 7 years ago
If you keep an eye on Indeed.com and Angel.co you&#x27;ll notice a decline in Scala adoption over the last few years. I can&#x27;t help thinking implicits are partly responsible for this. They&#x27;re certainly the main reason I ditched Scala. More than any other language there seem to be more exmaples, with Scala, of a team trying it then switching to something simpler.
评论 #17594873 未加载
hdermsalmost 7 years ago
I think implicits are fine as long as they&#x27;re used primarily for the typeclass pattern. The extension methods and conversions aspect of implicits should be confined to library DSLs
hnbrosephalmost 7 years ago
i wonder if implicits could present a security risk, insofar as the compiler may grab some [perhaps mistakenly in-scope] identifier which has sensitive data on it.
emodendroketalmost 7 years ago
In general I feel like implicits are not a great feature.
namuolalmost 7 years ago
(Would be helpful to include &quot;Scala&quot; somewhere in the title)
评论 #17590457 未加载
rajman187almost 7 years ago
Off topic but annoying (and pedantic on my part) nonetheless, &quot;begging the question&quot; does not mean what the author seems to think.
peeyekalmost 7 years ago
Scala is like Rust and Rust is like Scala. They are proud of their complexity.
评论 #17591424 未加载
评论 #17591179 未加载
评论 #17592514 未加载
评论 #17592212 未加载