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

Support main methods from traits in Scala 3.4 (200USD Bounty) #89

Closed
UnaiUribarri-TomTom opened this issue Aug 10, 2023 · 4 comments · Fixed by #142
Closed

Support main methods from traits in Scala 3.4 (200USD Bounty) #89

UnaiUribarri-TomTom opened this issue Aug 10, 2023 · 4 comments · Fixed by #142

Comments

@UnaiUribarri-TomTom
Copy link

UnaiUribarri-TomTom commented Aug 10, 2023


From the maintainer Li Haoyi: I'm putting a 200USD bounty on this issue, payable by bank transfer on a merged PR resolving this issue, including a unit test to avoid regression. The issue is straightforward to reproduce.


My Main object has grown too large and I want to split it. I tried to create a trait for each entry point and then making the Main object extends all those traits. But in such case, mainargs does not find any of the @main annotated entry points in any of the traits, while it worked flawlessly when all those methods were together in the same Main object.

trait CommandList {
  @main
  def list(@arg v: String): Unit = ???
}

trait CommandCopy {
  @main
  def copy(@arg from: String, @arg to: String): Unit = ???
}

object Main extends CommandList with CommandCopy {
  def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}

Is there any way to support this use case? Without having to duplicate the entry point in all of them, of course.

@lihaoyi
Copy link
Member

lihaoyi commented Aug 10, 2023

This seems reasonable. I thought it would work already, but I've never tried and I guess if you're opening this ticket it doesn't. Should be implementable

@lihaoyi
Copy link
Member

lihaoyi commented Jul 22, 2024

Seems this works in 2.13 but not in 3.4

@lihaoyi lihaoyi changed the title Support main methods from traits Support main methods from traits in Scala 3.4 Jul 22, 2024
@lihaoyi lihaoyi changed the title Support main methods from traits in Scala 3.4 Support main methods from traits in Scala 3.4 (200USD Bounty) Jul 22, 2024
@arainko
Copy link
Contributor

arainko commented Jul 22, 2024

I'll try to get this one working 😸

@arainko
Copy link
Contributor

arainko commented Jul 23, 2024

For an example like this one:

trait CommandCopy {
  @main
  def copy(@arg from: String, @arg to: String): Unit = ???
}

class Joined extends  CommandCopy {
  @main
  def test(@arg from: String, @arg to: String): Unit = ???

  val parser = ParserForMethods(this)
}

The issue manifests itself as a compiler crash:

[error] ## Exception when compiling 23 sources to /home/aleksander/repos/mainargs/out/mainargs/jvm/3.3.1/test/compile.dest/classes
[error] java.lang.AssertionError: assertion failed: missing outer accessor in class Joined
[error] scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[error] dotty.tools.dotc.transform.ExplicitOuter$.dotty$tools$dotc$transform$ExplicitOuter$$$outerParamAccessor(ExplicitOuter.scala:237)
[error] dotty.tools.dotc.transform.ExplicitOuter$OuterOps$.loop$1(ExplicitOuter.scala:474)
[error] dotty.tools.dotc.transform.ExplicitOuter$OuterOps$.path$extension(ExplicitOuter.scala:483)

The code that gets generated looks like this:

(new ParserForMethods[Joined](
  MethodMains.apply[Joined](
    List.apply[MainData[Any, Joined]](
      MainData.create[Any, Joined](
        "copy",
        new main(main.$lessinit$greater$default$1, main.$lessinit$greater$default$2),
        List.apply[ArgSig](
          ArgSig.create[String, Joined](
            "from",
            new arg(arg.$lessinit$greater$default$1, arg.$lessinit$greater$default$2, arg.$lessinit$greater$default$3, arg.$lessinit$greater$default$4, arg.$lessinit$greater$default$5, arg.$lessinit$greater$default$6),
            None
          )(StringRead), 
          ArgSig.create[String, Joined](
            "to",
            new arg(arg.$lessinit$greater$default$1, arg.$lessinit$greater$default$2, arg.$lessinit$greater$default$3, arg.$lessinit$greater$default$4, arg.$lessinit$greater$default$5, arg.$lessinit$greater$default$6),
            None
          )(StringRead)),
        ((b: Joined, params: [A >: Nothing <: Any] =>> Seq[A][Any]) =>
          CommandCopy.this.copy(
            Seq.apply[Seq[Any]](params).apply(0).apply(0).asInstanceOf[String],
            Seq.apply[Seq[Any]](params).apply(0).apply(1).asInstanceOf[String]
          )
        )),
      MainData.create[Any, Joined](
        "test",
        new main(main.$lessinit$greater$default$1, main.$lessinit$greater$default$2),
        List.apply[ArgSig](
          ArgSig.create[String, Joined](
            "from",
            new arg(arg.$lessinit$greater$default$1, arg.$lessinit$greater$default$2, arg.$lessinit$greater$default$3, arg.$lessinit$greater$default$4, arg.$lessinit$greater$default$5, arg.$lessinit$greater$default$6),
            None
          )(StringRead),
        ArgSig.create[String, Joined](
          "to",
          new arg(arg.$lessinit$greater$default$1, arg.$lessinit$greater$default$2, arg.$lessinit$greater$default$3, arg.$lessinit$greater$default$4, arg.$lessinit$greater$default$5, arg.$lessinit$greater$default$6),
          None
        )(StringRead)),
        ((`b₂`: Joined, `params₂`: [A >: Nothing <: Any] =>> Seq[A][Any]) =>
          Joined.this.test(
            Seq.apply[Seq[Any]](`params₂`).apply(0).apply(0).asInstanceOf[String],
            Seq.apply[Seq[Any]](`params₂`).apply(0).apply(1).asInstanceOf[String]
          )
        ))), (() => Joined.this))
    ): ParserForMethods[Joined])

The issue itself stems from this:

((b: Joined, params: [A >: Nothing <: Any] =>> Seq[A][Any]) =>
          CommandCopy.this.copy(
            Seq.apply[Seq[Any]](params).apply(0).apply(0).asInstanceOf[String],
            Seq.apply[Seq[Any]](params).apply(0).apply(1).asInstanceOf[String]
          )
        )),

...so my bet is that this shouldn't be CommandCopy.this.copy(...) but b.copy(...)

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