The scala document not match the right @main method define
See original GitHub issueCompiler version
scala 3.1.3
$ scalac --version
Scala compiler version 3.1.3 -- Copyright 2002-2022, LAMP/EPFL
$ sbt --version
sbt version in this project: 1.6.2
sbt script version: 1.6.2
Minimized code
https://docs.scala-lang.org/scala3/book/taste-hello-world.html The document show not right code, which not equals the java public static void main
which will cause sbt assembly can not know the entry method.
The current document:
@main def hello() = println("Hello, world!")
What the really equals code [as for java]
object main:
def main(args: Array[String]): Unit =
println("hello world")
Output
#java -jar target/scala-3.1.3/myapp-assembly-0.1.0-SNAPSHOT.jar
Error: Main method not found in class com.example.main, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Expectation
$ java -jar target/scala-3.1.3/myapp-assembly-0.1.0-SNAPSHOT.jar
hello world
@main
works without inside object class
;
so my request is: why @main
can not working the same , with or without the object main
The document told us: A @main annotated method can be written either at the top-level or in a statically accessible object. https://docs.scala-lang.org/scala3/reference/changed-features/main-functions.html
No , it is not right, it only can work at top level, if put @main
inside a statically accessible object, it won’t compile right
object main:
@main def hello(): Unit =
The generate class file
$ javap -c main.class
Compiled from "main.scala"
public final class info.bsmi.sc3.main {
public static void hello();
Code:
0: getstatic #13 // Field info/bsmi/sc3/main$.MODULE$:Linfo/bsmi/sc3/main$;
3: invokevirtual #19 // Method info/bsmi/sc3/main$.hello:()V
6: return
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top GitHub Comments
See also section “A.12.4 The Oberon Environment” in document Oberon Language Definition for people (much) younger than Martin and myself 😉
We want to have several main methods. The decision to name the class after the method is really the core of the design.