Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use macro for implicit summoning in typeclass companion objects for Cats/Algebra/Spire #1667

Closed
ctongfei opened this issue May 13, 2017 · 7 comments

Comments

@ctongfei
Copy link
Contributor

ctongfei commented May 13, 2017

I wrote a macro in this gist. This removes the boilerplate every companion object of typeclasses has (the def apply[A](implicit A: Ev[A]) = A method), and is faster: the method invocation is replaced by the actual typeclass instance filled by the macro.

@ctongfei
Copy link
Contributor Author

ctongfei commented May 13, 2017

And results in smaller jar size: the apply method no longer needs to be specialized.

@edmundnoble
Copy link
Contributor

Have you seen https://github.com/non/imp? It seems to do most of what you propose, except for the ImplicitSummoner traits. Paging @non, because the reason why we don't use imp in cats is escaping me.

@johnynek
Copy link
Contributor

johnynek commented May 14, 2017 via email

@edmundnoble
Copy link
Contributor

@johnynek I just, just ran benchmarks to test if the JIT is doing this. If it is, we have to consider that not every piece of code is JITted and it never is on its first runs. JMH is giving us potentially unrealistic results because of this; my benchmarks have CompilerControl.Mode.Exclude which will disallow the JIT. I posted these results at DarkDimius/imp-bench#4:

[info] Benchmark                                 Mode  Cnt   Score   Error  Units
[info] addable.ImplVsImplicitlyAddable.baseline  avgt   30  82.945 ± 2.765  ns/op
[info] addable.ImplVsImplicitlyAddable.explicit  avgt   30  73.834 ± 1.009  ns/op
[info] addable.ImplVsImplicitlyAddable.imply     avgt   30  74.103 ± 0.975  ns/op

@ctongfei
Copy link
Contributor Author

@edmundnoble I was not aware of the imp project before. But still we could use my ImplicitSummoner traits to reduce some boilerplates.

@diesalbla
Copy link
Contributor

Considering that the use of macros to generate boilerplate, such as done in #2925, it seems unlikely that this will be taken up.

@rossabaker
Copy link
Member

I agree, and the complexity only gets worse during the Scala 3 transition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants