Programming Languages Section

Scala

Card image
Post by Amina Delali, September 19th,2021

Some Facts

Scala is a multi-paradigm language: object-oriented and functional. It is a statically typed  language that compiles to bytecode that runs on the Java Virtual Machine, as well as to Javascript that runs on the browser. It also runs natively through the Scala Native compiler.  

It adds some features not available in the Java language as the use of closures, and nested functions. Besides the fact that in Scala, every variable is an object and every operation is a method call.

You can find mainly two versions for the Scala language (as for Python): Scala 2 and Scala 3. It has a precise syntax that is easy to learn. It is used in Data processing, distributed computing and web development.  It is also used by several companies such as LinkedIn, Twitter and Foursquare.



How to install it

  • on Windows:

    To install Scala on Windows 10 you can use Coursier:

    1. first install coursier, the Scala artifact fetcher. To install it follow the instructions available under the windows section of this installation page.
    2. then use coursier on the command line to install Scala:

      cs install scala3-compiler
      cs install scala3-repl

    3. to test the installation: launch the Scala console using this command from the command prompt:
      scala3-repl

    4. in the Sacala console, run the following command ( to load and run test.scala):
      :load test.scala

     
    For more details on other options of installation, you can go to this
    Scala installation page.

  • on ubuntu :

    As for Windows 10, you can use coursier to install Sacala on Ubuntu:

    1. download and install coursier from the terminal with the following command: $ curl -fLo cs "$(uname | tr LD ld)"
      $ chmod +x cs
      $ ./cs install cs
      $ rm cs

      $ echo "export PATH=$PATH:/home/username/.local/share/coursier/bin" >> ~/.bashrc
      $ source ~/.bashrc
    2. to check if Coursier is installed, you can run this command:  $ cs --version
    3. then, to install Scala you can use the commands that follow:  cs install scala3-compiler
      cs install scala3-repl
      cs setup
    4. the problem after launching cs setup command, is that it will install  scalac and scala  (compiler and runner) for the Scala 2 language version and not for Scala 3. To solve this issue, you can download the needed binaries for version 3 from this Github release Page: you can, for example, download  the scala3-3.0.0.tar.gz archive.
    5. after downloading the archive, extract it. Then copy the lib folder and the content of the bin folder to :/home/username/.local/share/coursier/ and :/home/username/.local/share/coursier/bin respectively. You can before making this copy, make a backup for the old coursier/bin folder. You can realize the extraction of the file, the backup of the bin folder, and the copy of the new file using theses commands: $ tar -xvf scala3-3.0.0.tar.gz
      $ mkdir ~/.local/share/coursier/bin-backup
      $ cp ~/.local/share/coursier/bin/* ~/.local/share/coursier/bin-backup
      $ cp -f scala3-3.0.0/bin/* ~/.local/share/coursier/bin/
    6. to know the installed version of Scala, just run one the following commands: $ scala3-compiler --version
      $ scala3-repl --version
      $ scalac --version
      $ scala --version

     

In the video below, the details of the installation of the Scalalanguage on Ubuntu.

The Hello World Example

To write, compile and run your first Hello World Code in Scala on Ubuntu, you can just follow these steps:

  1. first Create the “Hello.scala” file, and save it in the Home folder.
  2. write and save in that file the following
    @main def hello() = println("Hello world !")
  3.  open the terminal from the Home folder, then compile the previously written code with the  scalac command. It will generate 6 files, one of them is home.class . “home.class”  corresponds to the name of the defined method ‘hello’, and not to the name of the scala file.The command is as follow: $ scalac Hello.scala
  4. now, you can run the generated code from the terminal, using the scala command: $ scala hello
  5. You can run your code, directly without compiling it, using this command (don’t forget the scala extension): $ scala Hello.scala

For more details about this code, you can visit this documentation page.



Something to say ?

If you want to add something about the Scala language or about this post, please feel free to do it by commenting below 🙂 .