Skip to content

Avoid recursive inlining for product types #574

Open
@MartinHH

Description

@MartinHH

Somewhat related to / inspired by #565 :

Motivational example

//> using dep "com.softwaremill.magnolia1_3::magnolia:1.3.13+0-44422700+20250215-1851-SNAPSHOT"

import magnolia1._

trait TypeNameInfo[T] {
  def name: TypeInfo
}

object TypeNameInfo extends ProductDerivation[TypeNameInfo]:
  def join[T](ctx: CaseClass[TypeNameInfo, T]): TypeNameInfo[T] =
    new TypeNameInfo[T]:
      def name: TypeInfo = ctx.typeInfo

  given fallback[T]: TypeNameInfo[T] =
    new TypeNameInfo[T]:
      def name: TypeInfo = TypeInfo("", "Unknown Type", Seq.empty)

case class BigCaseClass(
  a1: Int, b1: Int, c1: Int, d1: Int, e1: Int, f1: Int, g1: Int, h1: Int, i1: Int, j1: Int,
  k1: Int, l1: Int, m1: Int, n1: Int, o1: Int, p1: Int, q1: Int, r1: Int, s1: Int, t1: Int,
  u1: Int, v1: Int, w1: Int, x1: Int, y1: Int, z1: Int,
  a2: Int, b2: Int, c2: Int, d2: Int, e2: Int, f2: Int, g2: Int, h2: Int, i2: Int, j2: Int,
  k2: Int, l2: Int, m2: Int, n2: Int, o2: Int, p2: Int, q2: Int, r2: Int, s2: Int, t2: Int,
  u2: Int, v2: Int, w2: Int, x2: Int, y2: Int, z2: Int
) derives TypeNameInfo

compilation fails with

[error] Maximal number of successive inlines (32) exceeded,
[error] Maybe this is caused by a recursive inline method?
[error] You can use -Xmax-inlines to change the limit.
[error] ) derives TypeNameInfo

Proposed improvement: use summonAll instead of recursion

When compiling with Scala 3.4.0 or higher (thanks to this commit), scala.compiletime.summonAll does not rely on recursive inlining anymore.

Hence, for a Mirror p, one can do summonAll[Tuple.Map[p.MirroredElemTypes, Typeclass]] to summon all instances of a Typeclass for the MirroredElemTypes without recursing towards the "Maximal number of successive inlines".

So, I propose to change the implementation for product types to rely on that instead of recursion. (I'd propose the same for sum types, but I would not know how to do that and still resolve the various Mirrors for a sealed trait hierarchy with nested sealed traits.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions