Featured image of post Scala Fundamental

Scala Fundamental

Learning Scala

Installation

  • using sdkman, get list of scala available for installation sdk list scala
  • to install scala sdk install scala
  • to check version scala --version

Getting Started

Using Scala REPL

Scala’s REPL (Read-Eval-Print Loop) is perfect for experimentation:

1
scala

Here is an example of using the REPL:

1
2
3
4
5
6
7
8
mw@mac ~ % scala # type scala to start the REPL
Welcome to Scala 3.7.3 (21.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.                           
scala> 1+1
val res0: Int = 2                                            
scala> "Hello, " + "Scala!"
val res1: String = Hello, Scala!
# press ctrl+D to exit

Using Text Editor

  • using any text editor of your choice, create a new file with the extension .scala
  • write your scala code in the file
  • save the file
  • to run the scala code, type scala <filename.scala> in the terminal
  • example:
    1
    2
    3
    4
    5
    6
    
    mw@mac scratch % cat Hello.scala 
    object Hello {
        def main(args: Array[String]): Unit = {
            println("Hello Scala")
        }
    }
    
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus