What is the difference between case object defined in scala repl and sbt console? - scala

When I declare case object in SBT Console, it creates module
scala> case object A
defined module A
And when I define it in Scala REPL, it creates object
scala> case object A
defined object A
Thanks in advance!

There is no difference in using the Scala REPL or SBT Console.
The difference you are seeing is probably because of Scala Version.
Cross check the Scala Version on Scala REPL and SBT Console it will be different as in Scala with version 10 series case object is defined as module but after Scala version 11 series they show object.
You can check the Scala version when you are opening the SBT Console or Scala REPL
You can also refer to this.
Hope this clears your doubt.
Thanks

Related

How can I get scala to work in the command line?

I run Windows 7.
I have java (a current enough version to run scala) and scala downloaded on my computer. I've set PATH so that when I type "scala" into the command prompt it sends me to the proper interface:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51)
Type in expressions to have them evaluated.
Type :help for more information
However I can't execute the command "scala" or "scalac" on my test files.
scala> scala Hello
(console):8: error: object Hello is not a member of package scala
This makes me think I'm in the wrong directory. The file Hello.scala is saved in the home directory that I set PATH to.
However I get a different issue when I try to compile code.
scala> scalac Hello.scala
(console):1: error: ';' expected but '.' found.
I actually got my test file to work at one point... but I wasn't actually IN scala.
C:\scala-2.9.1.final\bin> scala Hello.scala
Hello world!
I'm not really sure how to proceed from here. If anyone has any ideas of what may be wrong I would greatly appreciate input.
It appears that you're trying to run & compile programs from within the Scala REPL (read-evaluate-print loop - a kind of Scala interpreter) and you cannot do that in the REPL. The REPL allows you to type in Scala statements and see them execute immediately. (If you're not sure how you entered the REPL, you probably just entered the command scala from the command line.) The REPL is useful for testing ideas, and for experimenting with Scala. For example:
C:\some\path> scala
Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.
scala> println ("Hello!")
Hello!
scala> val x = 10
x: Int = 10
scala> val y = x * 5
y: Int = 50
scala> sys.exit
C:\some\path>
However, the REPL isn't what you would use to compile & run Scala programs - you need to do that from the command line (or from a tool such as sbt). If you want to run your program directly from the command line, without using the REPL (that is, without being in Scala, as you put it) then you would need to do the following:
Firstly, compile your program using scalac:
C:\some\path> scalac Hello.scala
If that succeeds, you can then run the program with the scala command (which looks for a Hello.class file):
C:\some\path> scala Hello
(Here C:\some\path is the location of the files Hello.scala & Hello.class.)
Alternatively, as you have already discovered, you can run your Scala program as a script in the REPL. You can do this from the command line as follows (note the addition of the filetype .scala after Hello compared to the command above):
C:\some\path> scala Hello.scala
or from within the REPL:
scala> :load Hello.scala
Hope this helps!
You don't have to issue scala command when you're inside REPL. If you want to execute code from that file, load it:
here is what I have in Foo.scala
println("I'm foo")
Now I'm starting the REPL (and as you can see scala> is a sign that you're ALREADY into REPL and can start execute raw scala code):
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :load Foo.scala
Loading Foo.scala...
I'm foo

Using eval to println elements of library-dependencies in SBT 0.12.x shell

Trying to master SBT and Scala makes my live tough at times when simple things in theory turn themselves into not so simple in practise.
I'm working with Apache Spark 0.9.0-incubating that uses SBT 0.12.4.
While poking around in their SBT build configuration I ran across an issue of how to println elements of library-dependencies in SBT (interactive) shell. It's a more general question of how to eval more complex Scala expressions in sbt shell?
> external-mqtt/library-dependencies
[info] List(org.scala-lang:scala-library:2.10.3, io.netty:netty-all:4.0.13.Final, org.eclipse.jetty:jetty-server:7.6.8.v20121106, org.eclipse.jetty.orbit:javax.servlet:2.5.0.v201103041518, org.scalatest:scalatest:1.9.1:test, org.scalacheck:scalacheck:1.10.0:test, com.novocode:junit-interface:0.9:test, org.easymock:easymock:3.1:test, org.mockito:mockito-all:1.8.5:test, commons-io:commons-io:2.4:test, commons-io:commons-io:2.4, org.eclipse.paho:mqtt-client:0.4.0)
I managed to do the following
> eval libraryDependencies in externalMqtt
[info] ans: sbt.SettingKey[Seq[sbt.ModuleID]] = sbt.Scoped$$anon$1#aa3170e
but am struggling with getting the Seq[sbt.ModuleID] out of sbt.SettingKey.
A concise explanation on how to proceed would be very appreciated. Thanks.
Instead of you using eval, if you first drop into:
consoleProject
then you can attach .eval to settings, so e.g.
> consoleProject
[info] Starting scala interpreter...
[info]
import sbt._
import Keys._
import currentState._
import extracted._
import cpHelpers._
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
scala> (fullClasspath in Compile).eval.foreach(println)
Attributed(/Users/tisue/Dropbox/repos/euler/target/scala-2.10/classes)
Attributed(/Users/tisue/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
This doesn't answer your question directly; I don't know how to make eval do what you want. As documented at http://www.scala-sbt.org/release/docs/Detailed-Topics/Console-Project.html , and as actually visible in the above transcript, consoleProject does a number of imports that bring useful names and implicits into scope, which is what makes the above work. You might expect that eval would evaluate code you pass it in a context where the same identifiers and implicits are in scope, but it doesn't. (I tried putting the same imports into a call into eval but it didn't work.)
Not the greatest answer — perhaps someone else can be more authoritative.

Does Scala having an interpreter give Scala projects the option to execute them either compiled or interpreted? [duplicate]

This question already has answers here:
The difference between scala script and application
(3 answers)
Closed 8 years ago.
I'm aware that scala has an interpreter and scala is statically typed. So I'm wondering if it is possible to execute scala projects in both Java and PHP style ?
Maybe you just need an interpreter to test your code? Then type scala to get interpreter and use :load command to load scala file.
I don't know exactly what is the PHP style, but yes, you can execute scala interactively, static typing is not a big issue here. If you need to exectue simple script that's easy (code from "Getting started in Scala"):
#!/bin/sh
exec scala "$0" "$#"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld.main(args)
If you have .jar dependencies in your script than things got more complicated,because you need to pass this jar dependencies to the scala interpreter. here is example
As of current date this method doesn't allow you to modularize scripts into the multiple files, but here is workaround
If you have sbt project you can type console from sbt shell to get a scala interpreter with correct classpath and dependencies. Also sbt itself has a 'script' mode which works quite like groovy's embedded dependency menegment.
Also scala compiler is embeddable) This project helps to dynamically compile/recompile scala files and load them into the jvm.

Compile and execute Scala code at runtime

Is is possible to compile and execute scala code as a string at runtime either in Scala or in Java?
My idea is to build a DSL using Scala then let Java programmers use the DSL inside Java.
I heard that the class scala.tools.nsc.Interpreter can do something like that, but when I imported it inside my scala file, I got "object tools is not a member of package scala."
So could anybody give me a hint?
In 2.10.0 we expose Scala reflection API, which among everything else includes a runtime compilation facility. More details can be found here: Generating a class from string and instantiating it in Scala 2.10.
I recommend you twitter-util's Eval
For scala3 this can be now achieved with dotty:
https://index.scala-lang.org/lampepfl/dotty
https://github.com/lampepfl/dotty
https://dotty.epfl.ch/docs/internals/overall-structure.html
The sbt dependency is e.g. "org.scala-lang" %% "scala3-compiler" % "3.1.3"

How to port Scala 2.7.7 code that uses scala.collection.jcl to Scala 2.8?

I've got some code that references scala.collection.jcl written against Scala 2.7.7. I'm now trying to compile it against Scala 2.8 for the first time, and I'm getting this error:
"value jcl is not a member of package collection".
Is there a substitute/replacement for jcl in 2.8?
It looks like JavaConversions does the job somewhat:
import scala.collection.JavaConversions._