From d8922070c507c3d59bca9db831a30fd6d8e19eec Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 26 Jan 2023 10:37:33 +0100 Subject: [PATCH] Make Parser covariant in Scala 3 (#453) --- .../src/main/scala-2/caseapp/core/parser/Internal.scala | 5 +++++ .../src/main/scala-3/caseapp/core/parser/Internal.scala | 5 +++++ .../shared/src/main/scala-3/caseapp/core/parser/Parser.scala | 2 +- .../src/main/scala/caseapp/core/complete/Completer.scala | 2 +- .../src/main/scala/caseapp/core/help/WithFullHelp.scala | 2 +- core/shared/src/main/scala/caseapp/core/help/WithHelp.scala | 2 +- .../src/main/scala/caseapp/core/parser/ParserMethods.scala | 3 ++- 7 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 core/shared/src/main/scala-2/caseapp/core/parser/Internal.scala create mode 100644 core/shared/src/main/scala-3/caseapp/core/parser/Internal.scala diff --git a/core/shared/src/main/scala-2/caseapp/core/parser/Internal.scala b/core/shared/src/main/scala-2/caseapp/core/parser/Internal.scala new file mode 100644 index 00000000..65cdc092 --- /dev/null +++ b/core/shared/src/main/scala-2/caseapp/core/parser/Internal.scala @@ -0,0 +1,5 @@ +package caseapp.core.parser + +private[parser] object Internal { + type uncheckedVarianceScala2 = scala.annotation.unchecked.uncheckedVariance +} diff --git a/core/shared/src/main/scala-3/caseapp/core/parser/Internal.scala b/core/shared/src/main/scala-3/caseapp/core/parser/Internal.scala new file mode 100644 index 00000000..fa3f7131 --- /dev/null +++ b/core/shared/src/main/scala-3/caseapp/core/parser/Internal.scala @@ -0,0 +1,5 @@ +package caseapp.core.parser + +private[parser] object Internal { + final class uncheckedVarianceScala2 extends scala.annotation.StaticAnnotation +} diff --git a/core/shared/src/main/scala-3/caseapp/core/parser/Parser.scala b/core/shared/src/main/scala-3/caseapp/core/parser/Parser.scala index 4b27cf14..aabc8660 100644 --- a/core/shared/src/main/scala-3/caseapp/core/parser/Parser.scala +++ b/core/shared/src/main/scala-3/caseapp/core/parser/Parser.scala @@ -12,7 +12,7 @@ import scala.language.implicitConversions * @tparam T: * success result type */ -abstract class Parser[T] extends ParserMethods[T] { +abstract class Parser[+T] extends ParserMethods[T] { import Parser.Step diff --git a/core/shared/src/main/scala/caseapp/core/complete/Completer.scala b/core/shared/src/main/scala/caseapp/core/complete/Completer.scala index cf879e18..78a98f99 100644 --- a/core/shared/src/main/scala/caseapp/core/complete/Completer.scala +++ b/core/shared/src/main/scala/caseapp/core/complete/Completer.scala @@ -3,7 +3,7 @@ package caseapp.core.complete import caseapp.core.Arg import caseapp.core.help.{WithFullHelp, WithHelp} -trait Completer[T] { self => +trait Completer[-T] { self => def optionName(prefix: String, state: Option[T]): List[CompletionItem] def optionValue(arg: Arg, prefix: String, state: Option[T]): List[CompletionItem] def argument(prefix: String, state: Option[T]): List[CompletionItem] diff --git a/core/shared/src/main/scala/caseapp/core/help/WithFullHelp.scala b/core/shared/src/main/scala/caseapp/core/help/WithFullHelp.scala index 9a2c6ca6..9cc21462 100644 --- a/core/shared/src/main/scala/caseapp/core/help/WithFullHelp.scala +++ b/core/shared/src/main/scala/caseapp/core/help/WithFullHelp.scala @@ -4,7 +4,7 @@ import caseapp.core.Error import caseapp.{ExtraName, Group, HelpMessage, Recurse} import caseapp.core.parser.Parser -final case class WithFullHelp[T]( +final case class WithFullHelp[+T]( @Recurse withHelp: WithHelp[T], @Group("Help") diff --git a/core/shared/src/main/scala/caseapp/core/help/WithHelp.scala b/core/shared/src/main/scala/caseapp/core/help/WithHelp.scala index 08c58475..d66e210e 100644 --- a/core/shared/src/main/scala/caseapp/core/help/WithHelp.scala +++ b/core/shared/src/main/scala/caseapp/core/help/WithHelp.scala @@ -14,7 +14,7 @@ import caseapp.core.Error * @tparam T: * type to which usage and help options are added */ -final case class WithHelp[T]( +final case class WithHelp[+T]( @Group("Help") @HelpMessage("Print usage and exit") usage: Boolean = false, diff --git a/core/shared/src/main/scala/caseapp/core/parser/ParserMethods.scala b/core/shared/src/main/scala/caseapp/core/parser/ParserMethods.scala index b2a2a021..3bf7d225 100644 --- a/core/shared/src/main/scala/caseapp/core/parser/ParserMethods.scala +++ b/core/shared/src/main/scala/caseapp/core/parser/ParserMethods.scala @@ -6,9 +6,10 @@ import caseapp.core.util.Formatter import caseapp.Name import caseapp.core.complete.Completer import caseapp.core.complete.CompletionItem + import scala.annotation.tailrec -trait ParserMethods[T] { parser: Parser[T] => +trait ParserMethods[+T] { parser: Parser[T @Internal.uncheckedVarianceScala2] => import Parser.Step