Skip to content

Commit 3a09496

Browse files
committed
Two refinements
- Communicate a single fully qualified wrapper method name instead of classname/methodname - Allow to add an annotation to a wrapper method
1 parent a929be5 commit 3a09496

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

tests/pos/main-method-scheme-generic.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import annotation.StaticAnnotation
1+
import annotation.{Annotation, StaticAnnotation}
22
import collection.mutable
33

44
/** MainAnnotation provides the functionality for a compiler-generated wrapper class.
@@ -60,17 +60,15 @@ trait MainAnnotation extends StaticAnnotation:
6060
def run(program: => MainResultType, mainName: String, docComment: String): CommandResult
6161
end Command
6262

63-
// Compile-time abstractions to control wrapper class:
64-
65-
/** The name to use for the static wrapper class
63+
/** The fully qualified name to use for the static wrapper method
6664
* @param mainName the fully qualified name of the user-defined main method
6765
*/
68-
inline def wrapperClassName(mainName: String): String
66+
inline def wrapperName(mainName: String): String
6967

70-
/** The name to use for the main method in the static wrapper class
71-
* @param mainName the fully qualified name of the user-defined main method
68+
/** An annotation type with which the wrapper method is decorated.
69+
* No annotation is generated of the type is left abstract.
7270
*/
73-
inline def wrapperMethodName(mainName: String): String
71+
type WrapperAnnotation <: Annotation
7472

7573
end MainAnnotation
7674

@@ -137,7 +135,9 @@ class main extends MainAnnotation:
137135

138136
def run(f: => MainResultType, mainName: String, docComment: String): Unit =
139137
def usage(): Unit =
140-
println(s"Usage: java ${wrapperClassName(mainName)} ${argInfos.map(_ + _).mkString(" ")}")
138+
val cmd = mainName.dropRight(".main".length)
139+
val params = argInfos.map(_ + _).mkString(" ")
140+
println(s"Usage: java $cmd $params")
141141

142142
def explain(): Unit =
143143
if docComment.nonEmpty then println(docComment) // todo: process & format doc comment
@@ -168,13 +168,15 @@ class main extends MainAnnotation:
168168
end run
169169
end command
170170

171-
inline def wrapperClassName(mainName: String): String =
172-
mainName.drop(mainName.lastIndexOf('.') + 1)
171+
inline def wrapperName(mainName: String): String =
172+
s"${mainName.drop(mainName.lastIndexOf('.') + 1)}.main"
173173

174-
inline def wrapperMethodName(mainName: String): String = "main"
174+
override type WrapperAnnotation = EntryPoint
175175

176176
end main
177177

178+
class EntryPoint extends Annotation
179+
178180
// Sample main method
179181

180182
object myProgram:
@@ -188,7 +190,7 @@ end myProgram
188190
// Compiler generated code:
189191

190192
object add extends main:
191-
def main(args: Array[String]) =
193+
@EntryPoint def main(args: Array[String]) =
192194
val cmd = command(args)
193195
val arg1 = cmd.argGetter[Int]("num", summon[ArgumentParser[Int]])
194196
val arg2 = cmd.argGetter[Int]("inc", summon[ArgumentParser[Int]], Some(1))

0 commit comments

Comments
 (0)