Scala option parsing with scopt - scala

Using scopt https://github.com/scopt/scopt
I have a very simple Scala CLI driver that errors out on the first line of .parse. The line is var i = 0, can’t imagine why that would fail, maybe in how i instantiated the OptionParser?
def parse(args: Seq[String], init: C): Option[C] = {
var i = 0 <———————————————— prints the error below
val pendingOptions = ListBuffer() ++ (nonArgs filterNot {_.hasParent})
Exception in thread "main" java.lang.NoSuchMethodError: scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;
at scopt.OptionParser.parse(options.scala:306)
at org.apache.mahout.drivers.ItemSimilarityDriver$.main(ItemSimilarityDriver.scala:47)
at org.apache.mahout.drivers.ItemSimilarityDriver.main(ItemSimilarityDriver.scala)
Full code here, sorry but I’m new to Scala so this may be a really stupid question
object ItemSimilarityDriver {
/**
* #param args Command line args, if empty a help message is printed.
* #return
*/
def main(args: Array[String]): Unit = {
val parser = new OptionParser[Config]("ItemSimilarity") {
head("ItemSimilarity", "Spark")
opt[Unit]('r', "recursive") action { (_, c) =>
c.copy(recursive = true) } text("The input path should be searched recursively for files that match the filename pattern (optional), Default: false.")
opt[String]('o', "output") required() action { (x, c) =>
c.copy(output = x) } text("Output is a path for all output (required)")
opt[String]('i', "input") required() action { (x, c) =>
c.copy(input = x) } text("Input is a path for input, it may be a filename or dir name. If a directory it will be searched for files matching the -p pattern. (required)")
note("some notes.\n")
help("help") text("prints this usage text")
}
// parser.parse returns Option[C]
parser.parse(args, Config()) map { config => <—————————— parser was created
but this call fails in the parse
// do stuff
//val didIt = true
} getOrElse {
// arguments are bad, error message will have been displayed, throw exception, run away!
}
}
case class Config(recursive: Boolean = false, input: String = null, output: String = null)
}
I've also tried the mutable options method with the same error.

The problem seems to be mismatch in Scala library version and scopt. The current stable scopt 3.2.0 is cross published against:
Scala 2.9.1
Scala 2.9.2
Scala 2.9.3
Scala 2.10
Scala 2.11
Scala 2.10 and 2.11 artifacts uses the sbt 0.12's cross versioning convention and uses _2.10 suffix because Scala 2.10.x minor releases are binary compatible with Scala 2.10.0. In other words scopt_2.11 is NOT a later version of scopt_2.10. One is compiled against Scala 2.11.x while the other Scala 2.10.x.
I'd recommend you give sbt a try to manage external libraries. sbt has a plugin to generate IntelliJ project for you.

Related

Understanding methods Vs functions in scala

I am learning the difference between methods and functions. I am following this link
http://jim-mcbeath.blogspot.co.uk/2009/05/scala-functions-vs-methods.html
The article says if you compile the following code:
class test {
def m1(x:Int) = x+3
val f1 = (x:Int) => x+3
}
We should get two files
1. test.class
2. test$$anonfun$1.class
But I do not get it. Secondly the example says if we execute the following command in REPL, we will get the below
scala> val f1 = (x:Int) => x+3
f1: (Int) => Int = <function>
But I get only this
scala> val f1 = (x:Int) => x+3
f1: Int => Int = $$Lambda$1549/1290654769#6d5254f3
Is it because we are using a different version? Please help.
Scala 2.11 and earlier versions behave as shown in the blog post.
The behavior changed in Scala 2.12. Scala now uses the lambda support that was added to version 8 of the JVM, so it doesn't need to emit the extra .class file. As a result, the .jar files produced by 2.12 are usually a lot smaller.
As a side effect of this, Scala can't override toString anymore, so you see the standard JVM toString output for lambdas.

Compilation issue of a Scala worksheet in IntelliJ

I have a worksheet containing
object test {
val tableTest = new Array[String](1)
tableTest.length
}
that returns
tableTest: Array[String] = [Ljava.lang.String;#628c5fdf
res0: Int = 1
and it seems ok.
However, when I enter this worksheet:
object test {
val tableTest = new Array[String](1)
tableTest(0) = "zero"
}
IntelliJ cannot compile and returns me a Unable to read an event from: rO0ABXNyADdvcmcuamV0YnJhaW5zLmpwcy5pbmNyZW1lbnRhbC... error.
Did I do something wrong?
I'm having the same issue with latest Idea and Scala plugin.
It seems that the worksheet has a problem executing any line which evaluates to Unit. Assigning is Unit, that's why your tableTest(0) = "zero" fails.
I've temporarily solved it with the following workaround:
this line will fail with error Error:Unable to read an event from:...
println("Will fail")
You can fix it by defining this helper method and using it for any Unit expression:
def unit(f: => Unit): String = {f; ""}
unit(println("Will work"))
You just have to ignore the line it generates in the output panel with res0: String =
You also can put this method in some object and import in any WS you need.
Gaston.
#ktonga

How to invoke the Scala compiler programmatically?

I want my Scala code to take a Scala class as input, compile and execute that class. How can I programmatically invoke a Scala compiler? I will be using the latest Scala version, i.e. 2.10.
ToolBox
I think the proper way of invoking the Scala compiler is doing it via Reflection API documented in Overview. Specifically, Tree Creation via parse on ToolBoxes section in 'Symbols, Trees, and Types' talks about parsing String into Tree using ToolBox. You can then invoke eval() etc.
scala.tools.nsc.Global
But as Shyamendra Solanki wrote, in reality you can drive scalac's Global to get more done. I've written CompilerMatcher so I can compile generated code with sample code to do integration tests for example.
scala.tools.ncs.IMain
You can invoke the REPL IMain to evaluate the code (this is also available in the above CompilerMatcher if you want something that works with Scala 2.10):
val main = new IMain(s) {
def lastReq = prevRequestList.last
}
main.compileSources(files.map(toSourceFile(_)): _*)
code map { c => main.interpret(c) match {
case IR.Error => sys.error("Error interpreting %s" format (c))
case _ =>
}}
val holder = allCatch opt {
main.lastReq.lineRep.call("$result")
}
This was demonstrated in Embedding the Scala Interpreter post by Josh Suereth back in 2009.
The class to be compiled and run (in file test.scala)
class Test {
println ("Hello World!")
}
// compileAndRun.scala (in same directory)
import scala.tools.nsc._
import java.io._
val g = new Global(new Settings())
val run = new g.Run
run.compile(List("test.scala")) // invoke compiler. it creates Test.class.
val classLoader = new java.net.URLClassLoader(
Array(new File(".").toURI.toURL), // Using current directory.
this.getClass.getClassLoader)
val clazz = classLoader.loadClass("Test") // load class
clazz.newInstance // create an instance, which will print Hello World.

Scala Actors in Scala 2.10 don't get recognized

I recently updated to Eclipse Juno, which also updated my Scala to 2.10. I know that Actors are deprecated now, but I'd still like to use them. The scala-actors.jar and scala-actors-migration.jar are part of my Eclipse build path, however, I get "object actors is not a member of package scala" for the imports. Any tries to re-add the scala library to the project failed didn't fix it either.
Mac OS X Lion with Eclipse Juno and Scala 2.10.
Some code (Marked the errors XXX):
import scala.actors.Actor XXX actors not found in scala
import scala.actors.Actor._ XXX actors not found in scala
class Solver() extends Actor { XXX Actor not found
def act() {
// Read file
val source = io.Source.fromFile("src/labyrinth.txt", "utf-8")
val lines = source.getLines.toList
source.close()
// Fill multidimensional array
var labyrinth = Array.ofDim[Cell](lines.length, lines.apply(0).length)
for (i <- 0 until lines.length) { // each line
for (j <- 0 until lines.apply(0).length) { // each column
labyrinth(i)(j) = new Cell(lines.apply(i).toList.apply(j)) // Fill array cell with character
}
}
// Search for start
for (k <- 0 until labyrinth(0).length) {
if (labyrinth(0)(k).character.toString() == "?") {
val start = new CheckCell(labyrinth, Array((1, k)), this).start // Kick off search
while (true) {
receive { XXX receive not found
case FoundSolution => XXX FoundSolution not found
Console.println("Pong: stop")
start ! Stop XXX Stop not found
exit()
}
}
}
}
}
}
Your example works fine for me (except the missing types, like Cell and CheckCell). I believe you might have one of the following:
a package scala.blah declaration in one of your source files
a source folder that contains scala as a sub-directory. Check that in Project Properties, Build Path and look for source folders. If you use the maven convention, src/main/scala should be a source directory (not just src/ or src/main/).
Both points above can shadow the standard scala package and replace it with your declaration (or directory), which obviously does not have an Actor type.

Is it possible to use scalap from a scala script?

I am using scalap to read out the field names of some case classes (as discussed in this question). Both the case classes and the code that uses scalap to analyze them have been compiled and put into a jar file on the classpath.
Now I want to run a script that uses this code, so I followed the instructions and came up with something like
::#!
#echo off
call scala -classpath *;./libs/* %0 %*
goto :eof
::!#
//Code relying on pre-compiled code that uses scalap
which does not work:
java.lang.ClassCastException: scala.None$ cannot be cast to scala.Option
at scala.tools.nsc.interpreter.ByteCode$.caseParamNamesForPath(ByteCode.
scala:45)
at scala.tools.nsc.interpreter.ProductCompletion.caseNames(ProductComple
tion.scala:22)
However, the code works just fine when I compile everything. I played around with additional scala options like -savecompiled, but this did not help. Is this a bug, or can't this work in principle? (If so, could someone explain why not? As I said, the case classes that shall be analyzed by scalap are compiled.)
Note: I use Scala 2.9.1-1.
EDIT
Here is what I am essentially trying to do (providing a simple way to create multiple instances of a case class):
//This is pre-compiled:
import scala.tools.nsc.interpreter.ProductCompletion
//...
trait MyFactoryTrait[T <: MyFactoryTrait[T] with Product] {
this: T =>
private[this] val copyMethod = this.getClass.getMethods.find(x => x.getName == "copy").get
lazy val productCompletion = new ProductCompletion(this)
/** The names of all specified fields. */
lazy val fieldNames = productCompletion.caseNames //<- provokes the exception (see above)
def createSeq(...):Seq[T] = {
val x = fieldNames map { ... } // <- this method uses the fieldNames value
//[...] invoke copyMethod to create instances
}
// ...
}
//This is pre-compiled too:
case class MyCaseClass(x: Int = 0, y: Int = 0) extends MyFactoryTrait[MyCaseClass]
//This should be interpreted (but crashes):
val seq = MyCaseClass().createSeq(...)
Note: I moved on to Scala 2.9.2, the error stays the same (so probably not a bug).
This is a bug in the compiler:
If you run the program inside an ide, for example Intellij IDEA the code is executed fine, however no fields names are found.
If you run it from command line using scala, you obtain the error you mentioned.
There is no way type-safe could should ever compiler and throw a runtime ClassCastException.
Please open a bug at https://issues.scala-lang.org/secure/Dashboard.jspa