score:13
you have different ways of running scala code:
first create a program with your classes, this is as in java, i use object because it works well without instantianing, like static, just compile with the sbt and run it you can also use the scala interpreter repl
we can use this object in the repl
scala> object hello {
| def main(args:array[string]) {
| println("hello, scala !!")
| }
| }
defined object hello
scala> hello.main(array("onlyforwork"))
hello, scala !!
compiling and running it using activator/sbt
> compile
[info] compiling 1 scala source to /home/anquegi/dev/stackoverflow/scalastack/target/scala-2.11/classes...
[success] total time: 2 s, completed 13/04/2015 11:29:42
> run
[warn] multiple main classes detected. run 'show discoveredmainclasses' to see the list
multiple main classes detected, select one to run:
[1] org.example.hello
[2] org.example.scheduledtaskscala
[3] question1.ques
[4] scriptworksheet.hello
enter number: 4
[info] running scriptworksheet.hello
hello, scala !!
[success] total time: 19 s, completed 13/04/2015 11:30:04
the second is that if we add the scala code as a script or file hello.scala, you can save your scala code in the file with .scala extension (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter
/**
* created by anquegi on 13/04/15.
*/
println("hello, scala !!")
if we call the scala interpreter this file is executed, you do not need to instanciate objects or clases, just executing like a shell script, you can also execute directlyy from intellij, but i use the console with scala installed on the system
[anquegi@localhost scalastack]$ scala src/main/scala/scriptworksheet/helloscript.scala
hello, scala !!
and finally the worksheet is the most powerfull, i recommend this for increasing your prodductivity at work bacause it is easy to test things is like the repl, ant it evluates the scala exprssions and shows you back the result
following is excerpt from official github repo wiki about the scala worksheet
a worksheet is a scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. worksheets are like a repl session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, auto-format, etc.
// we can define objects or classes and the worksheet
//will print the sesult of every expression
object hello {
def main(args:array[string]) {
println("hello, scala !!")
}
}
println("hello scala")
val a = 4 + 5
the result
defined module hello
hello scala
res0: unit = ()
a: int = 9
then a capture that shows you working with classe the work sheet and the console for scriptsin the intellij
score:12
scala worksheet
it's the same as scala interpreter (repl) but runs inside intellij. where you may easily and quickly evaluate some expressions. check intellij confluence page for more information.
scala script
if you don't want write script on bash you can do it with scala. it's just sequence of scala statements.
example:
import java.nio.file.{paths, files}
val scalasource = "*.scala"
val path = "path/to/necessary/folder"
val stream = files.newdirectorystream(paths.get(path), scalasource)
val paths = stream.iterator
while (paths.hasnext) {
println(paths.next.getfilename)
}
running it:
$ scala scala_script_name.scala
to getting started pick up this guide.
classes & object
short answer for scala classes it's similar to pojo and scala objects it's a java singleton class.
Source: stackoverflow.com
Related Query
- What is the difference between scala classes, scripts and worksheets in Intellij-idea?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- Scala: What is the difference between Traversable and Iterable traits in Scala collections?
- Scala - What is the difference between size and length of a Seq?
- What is the difference between a class and a type in Scala (and Java)?
- In Scala Akka futures, what is the difference between map and flatMap?
- What is the difference between scala StrictLogging and Lazylogging?
- Scala macros: What is the difference between typed (aka typechecked) and untyped Trees
- What is the difference between Expressions and Statements in Scala
- What is the difference between Array and WrappedArray in Scala
- What is the difference between Mongo Scala Driver and Reactive-Mongo Driver in play framework?
- What is the difference between Clojure REPL and Scala REPL?
- What is the difference between scala @Serializable and Java Serializable?
- What is the difference between curly bracket and parenthesis in a Scala for loop?
- Scala | what is the difference between '<-', '->' and '=>' operators and how do they work implicitly?
- What is the difference between Lisp macros and scala macros?
- What is the difference between ::: and :::.() in scala
- What is the difference between Clojure clojure.core.reducers/fold and Scala fold?
- What is the difference between SBT and IDEA when creating a Scala project in intelliJ?
- What is the difference between s"foo $bar" and "foo %s".format(bar) in scala
- What is the difference between hashmap and Listmap in scala
- What is the difference between should and must in scala testing?
- What is the difference and relationship between Scala Actors, Futures, Promises and Akka?
- What is the difference between case object defined in scala repl and sbt console?
- What is the difference between Scala's case class and class?
- What is the difference between self-types and trait subclasses?
- What is the difference between a var and val definition in Scala?
- What is the difference between "def" and "val" to define a function
- What is the difference between JavaConverters and JavaConversions in Scala?
- What are the key differences between Scala and Groovy?
More Query from same tag
- Scala code in JavaScript using Scala's '@'
- Convert RDD[(String, String, String)] to RDD[(String, (String, String))] in Spark Scala
- GUI programming with scala: what are the libraries and tools I can use?
- Scala List function for grouping consecutive identical elements
- Override equality for floating point values in Scala
- Is Tuple2 and Pair the same thing or not?
- How to add Google ML Kit dependency to SBT project?
- Scala - How to avoid if/else condition for object factory
- how to replace distinct() with reducebykey
- Scala and interfaces
- Programmatically enable sharding + choosing shard key on a collection using casbah with Mongo 2.4
- How do I declare a constructor for an 'object' class type in Scala? I.e., a one time operation for the singleton
- Circular dependency in Scala collections
- Converting a Scala Map to a List
- String.format causes an error
- Scalatest Scalamock behaves differently with same function declared two ways
- Difficulty creating spark.sql.Row from text file
- Stop Hook Is Not Being Called When Play Application Is Shutting Down
- An alternative to "while" for making repeated requests to a server
- Optionally modify head of sized list in shapeless
- Find the facility with the longest interval without accidents using Apache Spark SQL
- Implicit numeric widening in Scala 2.10, but not 2.11 or 2.12
- Synthesizing syntax tree and variable tree to avoid cross stage evaluation
- How to implement GetResult[List[String]] in scala slick?
- Casting the Dataframe columns with validation in spark
- build.sbt ProjectRef : not enough arguments for method apply
- Reading files from a directory in Scala
- Creating a new variable in Scala case block?
- Is there a way to auto close a spark/jupyter session
- NullPointerException...unable to figure out the reason