What kinds of macros should Scala 3 support?


#41

I agree with you.

I think it would be great if Scala 3 has ability to do it with black box macros.

May be It can be done with something like:
Pre-SIP: export, dual* of import

@RpcProxy
class  SomeThingProxy extend SomeThing{
  //Generate empty method for black box macros. 
  export SomeThing
}

#42

There is a new proposal for typelevel programming on the table, which is intended as a safer alternative for whitebox macros.

In a nutshell:

  • If you need to create new types and see these types in the same compilation run, transparent functions are the mechanism to use.

  • If you need to create new data and types for usage in a downstream projects, you can use principled meta programming and Tasty reflection.


#43

I like the new transparent mechanism quite a lot, and can see its power. The only thing that seems to be missing, offhand, is something equivalent to Generic and LabelledGeneric – a way to convert between the various sorts of strongly-typed products. Are there plans to add a mechanism for that?


#44

Wouldn’t the new breed of macro annotations (which generate code that becomes visible in dependent projects) fulfill this use case?


#45

Quite possibly, but I’m concerned that it may make applications structurally messy. Or possibly I’m misunderstanding how these macro annotations would come into play.

I guess the question is, if I am writing an application that is depending New Circe (however that works in the new world), can I do my serialization without having to break things down into multiple projects? I don’t mind things getting a little complex for libraries; I’m more concerned if that’s the case for routine applications.

That’s really the use case that I’d like to see fully-worked in the new world – my observation is that many problems seem to be Circe-complete. (That is, they turn out to want essentially the same machinery as Circe.) So if a consumer of New Circe could operate with reasonably minimal boilerplate, I’ll believe that many use cases are solved. But so far, I don’t quite grok how that would work in the new environment.


#46

No. Generics are currently generated on the fly for any case class and are available in the same project where case class is defined. Limiting them only to dependent projects would kill the entire ecosystem of typeclass derivation…


#47

In that case, I think we would need one standard Generic-like instance generated by the Scala compiler for each case class, so that libraries like circe can leverage it via implicits. And transparent methods could simplify this process (reducing the amount of implicit definitions needed). Additionally, it would be really cool if transparent methods and/or implicits could be cached somehow, so we don’t end up generating as much duplicated code as today.


#48

I think we would need one standard Generic-like instance generated by the Scala compiler for each case class

I’d rather that we retain a generic way to do compile-time reflection and surfacing that information in generated code - terms or types, rather than blessing one compiler-baked Generic implementation. Many projects rely on the ability to do their own compile-time and runtime reflection, such as beanpuree - https://github.com/limansky/beanpuree/blob/master/README.md - that inspects Java classes, not Scala classes, and as such can’t be served by scala-specific Generic, jsoniter - https://github.com/plokhotnyuk/jsoniter-scala - that relies on compile-time reflection and macros to generate very efficient low-level JSON parsing code, distage - https://github.com/pshirshov/izumi-r2 - our own project, hybrid runtime and compile-time dependency injection framework that crucially relies on TypeTags - but we’re also generating custom type tags for higher-kinded types - https://github.com/pshirshov/izumi-r2/blob/develop/distage/distage-model/src/main/scala/com/github/pshirshov/izumi/distage/model/reflection/universe/WithDITypeTags.scala

Additionally, it would be really cool if transparent methods and/or implicits could be cached somehow, so we don’t end up generating as much duplicated code as today.

Implicits cannot be cached because they’re not coherent and not pure. Transparent methods are all that + dependent on call site scope.


#49

We plan to put typeclass derivation in the language, by adding a scheme where typeclasses can be defined automatically for case class hierarchies. What transparent functions give us in this respect is that we can have a simple fold-based derivation mechanism and still get the full power of Generic and LabelledGeneric. At least I hope so - we still have to try that out.


#50

How to minimize def mImpl(x: Expr[T]) invocation?

If I understand correctly in the case:

transparent def m(x: T) = ~mImpl('x)
...
def mImpl(x: Expr[T]) = ...

The mImpl will be called in every usage of method m :

val m1 = m(1) //first call mImpl
val m2 = m(2) //second call mImpl

It can be very expensive.
Is it posible mImpl('x) to be called only in class definition?

transparent  trait Factory(val  a:T){
    transparent def apply(x: T) = ~mImpl('x,'a)
}

object  Factory extends Factory("type")

At compile time it transforms

object  Factory extends Factory("type"){
  transparent def apply(x: T) = x match {
    case 0 => ...
    case _ => ...
  }
}