Pre-SIP: Export Macros

There’s a very similar issue with type- and “term-aliases”.

So nothing new here, imho.

In case I understand the proposal correctly there is no big problem with tooling. The generated code would end up in some form on disk, wouldn’t it? Or is the generated code purely “virtual”?

Nevertheless there is imho a need for such a feature. Scala is boldly lacking an unrestricted code gen feature!

Languages like Java have that: Annotation pre-processors. (Don’t try to use that form Scala though if you’re not keen on much pain in the *piep*).

It’s imho quite “funny” that such a powerful language like Scala has no mechanism to cut down mechanical boilerplate when even a still boilerplate-hell language like Java can do that.

The current “solutions” in Scala emit concatenated Strings as code files… That approach couldn’t be more primitive and error prone! :face_exhaling:

Please @odersky consider some form of proper code gen for Scala! I know your gut feelings are mostly right, but this issue at hand needs a solution. A solution that’s worth being in such a great language as Scala. Show the world how an outstanding feature in such regard could look like instead of leaving it to hacks like “emitting code as plain strings to disk”.

This proposal here looks really promising. (Almost) nobody complained, but it got instantly 11 Likes, which is quite a lot for this forum. (I read this as: The proposal is so convincing everybody is just shouting “take my money”. Usually you hear “Bedenkenträger” even in case of otherwise great proposals). It’s even already implemented half way… @nicolasstucki the macro maintainer seems to like the idea presented here also as I infer form some GitHub comments.

Btw., regarding tooling for code gen: Maybe Scala could get some inspiration form C# here. They introduced a feature called “partial classes” to aid with auto-generated compilation units.

  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
  • When using source generators to generate additional functionality in a class.

I’m not sure this should be imitated verbatim. But it’s interesting food for thought.

Everybody involved here: Thanks for finally looking into such an important feature like code gen!