What is the Main object when running a Scala script? - scala

For example the following code:
object Test extends App {
val x = 100
println(Main.x+" "+x)
}
Seems invalid since it won't compile given there is no Main object define anywhere.
And effectively it doesn't compile in Scastie nor using a standard scalac compiler (version 2.13.4), nor if you run this code directly on the REPL.
However, if you save it to a file and then you run it using the scala command (same version as before); like a script, it runs without problems and produces the following output:
100 100
What is that Main object? When and why it was added?

From what I see scriptBody() is responsible for this behavior:
/** This is the parse entry point for code which is not self-contained, e.g.
* a script which is a series of template statements. They will be
* swaddled in Trees until the AST is equivalent to the one returned
* by compilationUnit().
*/
def scriptBody(): Tree = {
// remain backwards-compatible if -Xscript was set but not reasonably
settings.script.value match {
case null | "" => settings.script.value = "Main"
case _ =>
}
...
// pick up object specified by `-Xscript Main`
def mainModule: Tree = settings.script.valueSetByUser.map(name => searchForMain(TermName(name))).getOrElse(EmptyTree)
/* Here we are building an AST representing the following source fiction,
* where `moduleName` is from -Xscript (defaults to "Main") and <stmts> are
* the result of parsing the script file.
*
* {{{
* object moduleName {
* def main(args: Array[String]): Unit =
* new AnyRef {
* stmts
* }
* }
* }}}
*/
def repackaged: Tree = {
...
Basically your script body is forcefully rewritten into something that is
object Main {
// must contain main
}
We can confirm that -Xscript appearing in these methods indeed controls this behavior:
// test.scala
object Test extends App {
val x = 100
println(Main.x+" "+x)
println(Main.getClass)
new Exception().printStackTrace()
}
With defaults we see that "Main" is used and that code is rewritten to have Main as starting point (code reused existing object Test extends App to create it):
> scala test.scala
warning: 1 deprecation (since 2.13.0); re-run with -deprecation for details
100 100
class Main$
java.lang.Exception
at Main$.delayedEndpoint$Main$1(test.scala:5)
at Main$delayedInit$body.apply(test.scala:1)
at scala.Function0.apply$mcV$sp(Function0.scala:39)
at scala.Function0.apply$mcV$sp$(Function0.scala:39)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
at scala.App.$anonfun$main$1(App.scala:76)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at Main$.main(test.scala:1)
at Main.main(test.scala)
...
If we specify Test as our main, Main is not generated and compilation fails
> scala -Xscript Test test.scala
test.scala:3: error: not found: value Main
println(Main.x+" "+x)
^
test.scala:4: error: not found: value Main
println(Main.getClass)
Behavior of e.g. Scastie is different as Scastie defines its own main and put your code into it, so this synthetic Main is never created (though Test was still tampered with).

Related

sbt could not find implicit value for parameter valName

I'm using sbt to build some of the riscv boom from the source code, but the sbt complains that it "could not find implicit value for parameter valName: : freechips.rocketchip.diplomacy.ValName". The detailed error message are as below:
[error] F:\hiMCU\my_proj\src\main\scala\freechips\rocketchip\tile\BaseTile.scala:170:42: could not find implicit value for parameter valName: freechips.rocketchip.diplomacy.ValName
[error] Error occurred in an application involving default arguments.
[error] protected val tlMasterXbar = LazyModule(new TLXbar)
The code where sbt complains is as below:
abstract class BaseTile private (val crossing: ClockCrossingType, q: Parameters)
extends LazyModule()(q)
with CrossesToOnlyOneClockDomain
with HasNonDiplomaticTileParameters
{
// Public constructor alters Parameters to supply some legacy compatibility keys
def this(tileParams: TileParams, crossing: ClockCrossingType, lookup: LookupByHartIdImpl, p: Parameters) = {
this(crossing, p.alterMap(Map(
TileKey -> tileParams,
TileVisibilityNodeKey -> TLEphemeralNode()(ValName("tile_master")),
LookupByHartId -> lookup
)))
}
def module: BaseTileModuleImp[BaseTile]
def masterNode: TLOutwardNode
def slaveNode: TLInwardNode
def intInwardNode: IntInwardNode // Interrupts to the core from external devices
def intOutwardNode: IntOutwardNode // Interrupts from tile-internal devices (e.g. BEU)
def haltNode: IntOutwardNode // Unrecoverable error has occurred; suggest reset
def ceaseNode: IntOutwardNode // Tile has ceased to retire instructions
def wfiNode: IntOutwardNode // Tile is waiting for an interrupt
protected val tlOtherMastersNode = TLIdentityNode()
protected val tlSlaveXbar = LazyModule(new TLXbar)
protected val tlMasterXbar = LazyModule(new TLXbar)
protected val intXbar = LazyModule(new IntXbar)
....
}
The LazyModule object code is as below:
object LazyModule
{
protected[diplomacy] var scope: Option[LazyModule] = None
private var index = 0
def apply[T <: LazyModule](bc: T)(implicit valName: ValName, sourceInfo: SourceInfo): T = {
// Make sure the user put LazyModule around modules in the correct order
// If this require fails, probably some grandchild was missing a LazyModule
// ... or you applied LazyModule twice
require (scope.isDefined, s"LazyModule() applied to ${bc.name} twice ${sourceLine(sourceInfo)}")
require (scope.get eq bc, s"LazyModule() applied to ${bc.name} before ${scope.get.name} ${sourceLine(sourceInfo)}")
scope = bc.parent
bc.info = sourceInfo
if (!bc.suggestedNameVar.isDefined) bc.suggestName(valName.name)
bc
}
}
I think the sbt should find some val of type freechips.rocketchip.diplomacy.ValName, but it didn't find such kind of val.
You need to have an object of type ValName in the scope where your LazyModules are instantiated:
implicit val valName = ValName("MyXbars")
For more details on Scala's implicit please see https://docs.scala-lang.org/tutorials/tour/implicit-parameters.html.html
You generally shouldn't need to manually create a ValName, the Scala compiler can materialize them automatically based on the name of the val you're assigning the LazyModule to. You didn't include your imports in your example, but can you try importing ValName?
import freechips.rocketchip.diplomacy.ValName
In most of rocket-chip code, this is imported via wildcard importing everything in the diplomacy package
import freechips.rocketchip.diplomacy._

scala inherited value do not find

Scala version 2.11.8
I have parent class
abstract class FR(externalId:String, code:String, message:String) extends Serializable {
val this.externalId=externalId;
val this.code = code;
val this.message = message;
def toString:String={
return "FRworks";
}
}
The child class is:
class RD extends FR {
def this(lpTransaction:LPTransaction)={
externalId =lpTransaction.getField("somethinghere").toString
...
}
}
The error is:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from F:\workspace\frankcheckAPI\project
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/)
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes...
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\RD.scala:9: 'this' expected but identifier found.
[error] externalId =lpTransaction.getField("somethinghere").toString
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
when I add this in front of externalId the error still:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from F:\workspace\frankcheckAPI\project
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/)
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes...
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:9: '}' expected but '.' found.
[error] this.externalId =lpTransaction.getField("somethinghere").toString
[error] ^
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:12: eof expected but '}' found.
[error] }
[error] ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
Your code is very Java-influenced. There are couple of things wrong here; when you fix the obvious ones like missing class parameters in RD it boils down to not being able to reassign to val.
Let me give you an improved, Scala-fied version of the whole code.
abstract class FR(val externalId: String, val code: String, val message: String) extends Serializable
// dummy class
class LPTransaction {
def getField(s: String) = s
}
class RD(externalId: String, code: String, message: String) extends FR(externalId, code, message) {
def this(lpTransaction: LPTransaction) = {
this(lpTransaction.getField("somethinghere").toString, "defaultCode", "defaultMessage")
}
println(externalId)
}
val a = new RD(new LPTransaction) // prints "somethinghere"
Main improvements are:
You don't need private fields to be populated using arguments in the constructor. Scala favors immutability. Make your class arguments "vals", this means they will be available as public fields (instead of getters you will access them directly; this is contrary to OOP's encapsulation principle, but here it's ok because nobody can mess with them anyway since they are immutable; they may only be fetched)
Your subclass RD should be taking same fields as parameters as its parent class. Of course, you can then define an auxiliary constructor that takes only LPTransaction, but then you need to feed the parent class with some default values for the other parameters.
The rest kind of follows from this. I added the dummy implementation of LPTransaction to be able to compile. I also threw in a println statement in RD class just for the sake of example. Feel free to ask if something's not clear.
//scala automatically generates getters for passed in params. No need to set them explicitly. For immutable params use val, for mutable use var
abstract class FR(var externalId:String, val code:String, val message:String) extends Serializable {
//need to use override annotation for superclass methods
override def toString:String={
return "FRworks";
}
}
// notice how constructor parameters are passed to the base class when defining the child class.
class RD extends FR("someID","code","msg") {
def printId() = println(externalId)
}
val x = new RD
x.externalId = "new ID" //works because externalId is var (mutable)
x.code = "new code" //error because code is val (immutable)
x.printId //correctly prints the external id: someID

Main method invocation in scala nsc

I was trying to go through the code for nsc(new scala compiler). I am a little confused about Main.scala. It is implemented as follows:
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
* #author Martin Odersky
*/
package scala.tools
package nsc
import scala.language.postfixOps
/** The main class for NSC, a compiler for the programming
* language Scala.
*/
class MainClass extends Driver with EvalLoop {
def resident(compiler: Global): Unit = loop { line =>
val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError))
compiler.reporter.reset()
new compiler.Run() compile command.files
}
override def newCompiler(): Global = Global(settings, reporter)
override def doCompile(compiler: Global) {
if (settings.resident) resident(compiler)
else super.doCompile(compiler)
}
}
object Main extends MainClass { }
My first question is, how is Main being called by the compiler process? When I call something along the lines of:
scalac [ <options> ] <source files>
Somewhere along the lines the newCompiler and doCompile is being called, can somebody help me with tracing through how this is being called and how the compiler is being initialized?
Any pointers will be much appreciated.
Thanks
MainClass extends Driver which has the main method:
def main(args: Array[String]) {
process(args)
sys.exit(if (reporter.hasErrors) 1 else 0)
}
At the same time, object Main extends MainClass which means that there is a Main.class file that contains a public static void main(String[] args) forwarder method that actually invokes aforementioned non-static method on object Main. See for example this question for more details on how object is compiled in Scala.
This means that scala.tools.nsc.Main can be used as a main class when running the scala compiler (this is hardcoded somewhere in the scalac script).
newCompiler and doCompile are called by process.

Explain why import scala.Predef.String fails build

I found very strange issue with Scala import.
I wrote sample class:
package test_scala_predef
object App extends App {
classOf[T]
println( "Hello World!" )
}
class T {
}
This class compiles without any errors.
But, if I add
import scala.Predef.String
then I get compilation errors:
[INFO] Compiling 1 source files to /home/uthark/src/_/test_scala_predef/target/classes at 1374028063588
[ERROR] /home/uthark/src/_/test_scala_predef/src/main/scala/test_scala_predef/App.scala:10: error: not found: value classOf
[INFO] classOf[T]
[INFO] ^
[ERROR] /home/uthark/src/_/test_scala_predef/src/main/scala/test_scala_predef/App.scala:11: error: not found: value println
[INFO] println( "Hello World!" )
[INFO] ^
[ERROR] two errors found
I have an idea, that after I add specific import from scala.Predef, then implict import of scala.Predef._ is not added. But I can't find anything about it in the Scala documentation. Can anyone point me to the relevant section in documentation?
I checked Scala Language Specification (PDF), section 12.5 covering scala.Predef but also didn't find any related.
I use latest stable scala version available (2.10.2 at the moment)
I found the answer in the sources.
https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/typechecker/Contexts.scala:
/** List of symbols to import from in a root context. Typically that
* is `java.lang`, `scala`, and [[scala.Predef]], in that order. Exceptions:
*
* - if option `-Yno-imports` is given, nothing is imported
* - if the unit is java defined, only `java.lang` is imported
* - if option `-Yno-predef` is given, if the unit body has an import of Predef
* among its leading imports, or if the tree is [[scala.Predef]], `Predef` is not imported.
*/
protected def rootImports(unit: CompilationUnit): List[Symbol] = {
assert(definitions.isDefinitionsInitialized, "definitions uninitialized")
if (settings.noimports) Nil
else if (unit.isJava) RootImports.javaList
else if (settings.nopredef || treeInfo.noPredefImportForUnit(unit.body)) {
debuglog("Omitted import of Predef._ for " + unit)
RootImports.javaAndScalaList
}
else RootImports.completeList
}
This answers my question.
The key sentence is this:
Exceptions: ... if the unit body has an import of Predef among its leading imports
Also, in chat on #scala irc it was suggested to create bug in Scala issue tracking system, so I did it. https://issues.scala-lang.org/browse/SI-7672

Program works when run with scala, get compile errors when try to compile it with scalac

I am testing the code below, does a basic database query. It works fine when I run it from the CLI using "scala dbtest.scala", but gives me compile errors when I try to compile it with scalac :
[sean#ibmp2 pybackup]$ scalac dbtest.scala
dbtest.scala:5: error: expected class or object definition
val conn_str = "jdbc:mysql://localhost:3306/svn?user=svn&password=svn"
^
dbtest.scala:8: error: expected class or object definition
classOf[com.mysql.jdbc.Driver]
^
dbtest.scala:11: error: expected class or object definition
val conn = DriverManager.getConnection(conn_str)
^
dbtest.scala:12: error: expected class or object definition
try {
^
four errors found
import java.sql.{Connection, DriverManager, ResultSet};
import java.util.Date
// Change to Your Database Config
val conn_str = "jdbc:mysql://localhost:3306/svn?user=xx&password=xx"
// Load the driver
classOf[com.mysql.jdbc.Driver]
// Setup the connection
val conn = DriverManager.getConnection(conn_str)
try {
// Configure to be Read Only
val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
// Execute Query
val rs = statement.executeQuery("SELECT * FROM backup")
// Iterate Over ResultSet
var svnFiles = Set[String]()
while (rs.next) {
val repos = rs.getString("repos")
val lm = rs.getDate("lastModified")
val lb = rs.getDate("lastBackedup")
if (lm.getTime() > lb.getTime()) {
println(repos + " needs backing up")
svnFiles += repos
}
else {
println(repos + " doesn't need backing up")
}
}
println(svnFiles)
}
finally {
conn.close
}
You need either a class, object, or trait at the top level to make it a legal source to compile. scala interpreter expects definitions and expressions, whereas scalac expects something that can turn into Java .class files.
//imports here
object DbTest {
def main(args: Array[String]) {
// your code here
}
}
Create a file called HelloWorld.scala, and enter the following:
object HelloWorld {
def main(args: Array[String]){
println("Hello World")
}
}
To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.
From the command line, run:
scalac HelloWorld.scala
This will generate a few class files in the current directory. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command.
Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output:
Now run:
scala HelloWorld.scala
Now "Hello World", will be printed to the console.
After researching this functionality, I found an article, which explains this in detail, and posted that information here on SO to help programmers understand this aspect of Scala development.
Source: http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html