Skip to content

Some suggestions about Scala script example #1133

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

Closed
counter2015 opened this issue Apr 10, 2020 · 2 comments
Closed

Some suggestions about Scala script example #1133

counter2015 opened this issue Apr 10, 2020 · 2 comments

Comments

@counter2015
Copy link
Contributor

In the doc website: https://www.scala-lang.org/documentation/your-first-lines-of-scala.html

It shows a example of Scala script.

#!/usr/bin/env scala

object HelloWorld extends App {
  println("Hello, world!")
}
HelloWorld.main(args)

When I try to run it, the compiler returns a warning.

$ ./script.sh 
./script.sh:6: warning: Script has a main object but statement is disallowed
HelloWorld.main(args)
               ^
Hello, world!

In fact without HelloWorld.main(args), it can also run while has access to args

#!/usr/bin/env scala

object HelloWorld extends App {
  println(s"Hello, world! ${args.mkString}")
}
$ ./script.sh Hello, Scala Script
Hello, world! Hello,ScalaScript

On the other hand, I found another Scala shell script example

In this one I can pass Scala comand line arguement such as -feature

#!/bin/sh
exec scala -feature -deprecation "$0" "$@"
!#

object Test {
  def main(args: Array[String]): Unit = {
    implicit def strToSymbol(str: String): Symbol = Symbol(str)
    println("hello, world")
  }
}

In this way, it will show me details about warning.

$ ./test.scala 
./test.scala:7: warning: implicit conversion method strToSymbol should be enabled
by making the implicit value scala.language.implicitConversions visible.
This can be achieved by adding the import clause 'import scala.language.implicitConversions'
or by setting the compiler option -language:implicitConversions.
See the Scaladoc for value scala.language.implicitConversions for a discussion
why the feature should be explicitly enabled.
    implicit def strToSymbol(str: String): Symbol = Symbol(str)
                 ^
hello, world

In my humble opinion, I have two suggestions:

  • Remove HelloWorld.main(args) from the example since it will cause a warning.
  • Using second header of script header since it can pass command line arguements of Scala.
@SethTisue
Copy link
Member

Agree on both counts. Pull requests highly welcome.

@counter2015
Copy link
Contributor Author

@SethTisue Thanks for your reply. I have made a PR here.

SethTisue pushed a commit that referenced this issue Apr 14, 2020
See #1133 , Remove `HelloWorld.main(args)` and change script header.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants