Skip to content

Incorrect warning when object contains multiple main methods #6896

@scabug

Description

@scabug

When an object contains multiple methods named main, including one with a valid type signature for the "true" main method, the compiler issues a warning indicating that the object contains no such method.

Here's an example:

object TooManyMains {                                                                                                                       
  def main(args: Array[String]): Unit = {                                                                                                   
    println("Hello, World!")                                                                                                                
  }                                                                                                                                         
  def main(a: Int, b: Int) = ???                                                                                                            
  def main(s: String, n: String) = ???                                                                                                      
}    
scalac mains.scala
mains.scala:1: warning: TooManyMains has a main method with parameter type Array[String], but TooManyMains will not be a runnable program.
  Reason: main method must have exact signature (Array[String])Unit
object TooManyMains {
       ^
one warning found

However it will will run successfully:

scala TooManyMains
Hello, World!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions