freestyle icon indicating copy to clipboard operation
freestyle copied to clipboard

SPIKE: Replacements for scalameta-based macros

Open diesalbla opened this issue 6 years ago • 15 comments

Issue #73, carried out in PR #344, replaced the first version of @free and @module, based on the Scala Macro annotations with the macro annotations provided by the Scalameta-Paradise project. That is the basis for the current implementations of @free, @module, @tagless, as well as those macros from the freestyle-rpc project.

However, the development of macros and macro annotations in Scala Meta was closed, as explained here.

With these lessons learned, we decided to retire our efforts to build a macro system on top of Scalameta.

Recently, It has come to our attention that the earliest release of Scala, 2.12.5, can not be used with Freestyle. Thus, we need to find out a replacement for those macro annotations.

Requirements

Given that freestyle is intended to help as many Scala developers as possible, many of whom use IntelliJ-Idea, the replacement should inter-operate fairly with IDEA. In particular, spurious errors should be avoided as far as fairly possible.

Proposals

  • Compiler Plugin: one proposal would be to develop a compiler plugin. Inside the scalaz-deriving project, `@fommil has recently developed a helper class for writing code-generation annotations.

Note: this issue will be extended, as the discussion progress.

diesalbla avatar Mar 26 '18 19:03 diesalbla

@diesalbla writing a separate intellij plugin is not difficult. I did it already for @deriving and it was quite easy. https://github.com/JetBrains/intellij-scala/pull/388

The code has changed a little bit since then (for the better). You can also look at simulacrum and monocle for inspiration.

fommil avatar Mar 26 '18 20:03 fommil

Another proposal is to rely entirely in codegen since our additions are mostly implicits and companion extensions and to do that we don't need macros we can just codegen our expansion as other projects do https://github.com/olafurpg/scalameta-sbt-codegen. Codegen expansions should work fine in IDEA

raulraja avatar Mar 26 '18 21:03 raulraja

I'm not sure codegen works for companions... a companion must be in the same file.

fommil avatar Mar 26 '18 21:03 fommil

(unless the companion extends from some trait that you define in the codegen)

fommil avatar Mar 26 '18 21:03 fommil

We can potentially skip companions since they are used now to add the Handler and Op dependent types but those could be just namespaced to the trait definition:

trait MyAlgebra[F[_]]
//codegen
trait MyAlgebraHandler

The other reason they are in use too is to get automatic resolution of implicit factories but those could be added to an implicits object automatically for all algebras under a package similar to how cats handles implicit traits aggregated in an all object which is also part of cats.implicits._

raulraja avatar Mar 26 '18 22:03 raulraja

Is there anything we can do to help with this? We made a big bet on freestyle in our code base. Hope there is a way forward for us to update scala version. We stuck with scala 2.12.4 for now.

ngbinh avatar May 02 '18 07:05 ngbinh

@ngbinh, It looks like paradise has been published for 2.12.6, see https://github.com/scalamacros/paradise/issues/116.

mwz avatar May 02 '18 09:05 mwz

@mwz freestyle is using https://github.com/scalameta/paradise/, a different project that has been abandoned

ngbinh avatar May 02 '18 10:05 ngbinh

I think that scalameta paradise is also getting released against 2.12.6, https://github.com/scalameta/scalameta/issues/1515 and https://gitter.im/scalameta/scalameta?at=5ae3661b62316e0505089ffe (they skipped a 2.12.5 release due to macro regressions).

mwz avatar May 02 '18 10:05 mwz

Would be awesome if it's happening. Thanks, will be tracking that ticket

ngbinh avatar May 02 '18 11:05 ngbinh

My recommendation would be to use scalamacros/paradise and implement custom support for IntelliJ. scalamacros/paradise are on track to be included by default in the Scala compiler https://github.com/scala/scala/pull/6606 (no more need for addCompilerPlugin) and @mutcianm had a great presentation at ScalaSphere about how to add support for custom macros in IntelliJ, it's easier than most people think.

olafurpg avatar May 07 '18 10:05 olafurpg

@mutcianm had a great presentation at ScalaSphere about how to add support for custom macros in IntelliJ, it's easier than most people think.

Is there an online link for this talk?

soronpo avatar May 07 '18 12:05 soronpo

I don't think the recordings are available yet. Typically the talks are uploaded to YT a couple months later, so I'd expect them to be public soon.

mutcianm avatar May 10 '18 14:05 mutcianm

FWIW, scalameta/paradise for 2.12.6 is on it's way to Maven Central. Note that scalameta/paradise is no longer under development and will not support 2.13, please migrate to https://docs.scala-lang.org/overviews/macros/annotations.html those macro annotations will be supported natively by the compiler in 2.13 under the scalac option -Ymacro-annotations https://github.com/scala/scala/pull/6606

olafurpg avatar May 25 '18 07:05 olafurpg

Just curious if this issue is still progressing.

kthompson avatar Aug 24 '18 16:08 kthompson