My first program

Hello
I am new to Scala
I have the following code
def whileLoop(): Unit = {
var i = 1
while (i <= 3) {
println(i)
i += 1
}
}
whileLoop()
can I run it directly or I must have a project?
Thanks in advanced

1 Like

Hey, you’ll need to define a main method, in your case it might be (when using Scala 3)

@main def MyMain = whileLoop()

if your’re using Scala 2 then you’ll need a bit more boilerplate

object MyMain {
  def main(args: Array[String]): Unit = whileLoop()
}

// or this one, but it's partially deprecated
```scala
object MyApp extends App {
  whileLoop()
}

Then you can run it using scala main.scala

As soon as you’ll want to add external dependencies or create tests take a look at scala-cli https://scala-cli.virtuslab.org/ it’s a new runner for Scala that manages also dependencies, ensures that JVM is installed and contains everything you might need when learning Scala.

1 Like

The scala command in Scala 2 will run scripts just like you have it. As @WojciechMazur pointed out, you can also extend this to be an application or use scala-cli. Honestly, I would recommend scala-cli these days.

If you are using Scala3, you could run this code using the scala command

@main def whileLoop(): Unit = {
  var i = 1
  while (i <= 3) {
    println(i)
    i += 1
  }
}

This version of the code works with current installs of scala and scala-cli.

mlewis@Linux-Desktop ~/Test $ scala while.scala 
1
2
3
mlewis@Linux-Desktop ~/Test $ scala-cli while.scala
1
2
3
1 Like

Small programs like yours:

def whileLoop(): Unit = {
  var i = 1
  while (i <= 3) {
    println(i)
    i += 1
    }
  }
whileLoop()

can also directly be tested and run in Scastie.

2 Likes

If you’re just playing around with ideas and use an IDE, you can use a worksheet: https://docs.scala-lang.org/scala3/book/tools-worksheets.html.

I use these for making bug reproduction reports or as reminders of coding techniques.

As @devlaam mentioned, there is also Scastie for this. If you want to publish your code as part of a discussion it’s handy, especially if you want others to provide answers as code.

Try both and take your pick.

1 Like

Thanks a lot.
I do it but I still have errors as follows:
[E103] Syntax Error:
10 |whileLoop()
|^^^^^^^^^
|Illegal start of toplevel definition
|
| longer explanation available when compiling with -explain
1 error found
Errors encountered during compilation
Here is my full code:


def whileLoop(): Unit = {
  var i = 1
  while (i <= 3) {
    println(i)
    i += 1
  }
}
whileLoop()```

Sorry. I forgot to mention I use scala 3 and here is the full version of my code
The full code as follows:


def whileLoop(): Unit = {
  var i = 1
  while (i <= 3) {
    println(i)
    i += 1
  }
}
whileLoop()

How to run the code on scala-cli from VS Code?

There whileLoop() is being used at the top-level.
In .scala files that’s not allowed (it’s not like Python).

You have 2 options:

  1. Add @main to your def, remove whileLoop() at the end. Then you can use scala-cli run myFile.scala on the Terminal.
// this is myFile.scala
// I wrote it in a more Scala 3 style:

@main
def whileLoop(): Unit = 
  var i = 1
  while i <= 3 do
    println(i)
    i += 1
  1. You can use worksheets. They end with .worksheet.sc file extension instead of .scala. In worksheets, toplevel usage is allowed, and it’s evaluated / printed directly on the editor. Here is an example in Scastie:
    Scastie - An interactive playground for Scala.

You can do the same in VS Code.

1 Like

Thanks a lot for your response and illustration.
But Can I press ctrl + F5 directly or green triangle directly?
Thanks & Regards

The second option work but the first doesn’t work

You need to remove the whileLoop() at the bottom.
Also make the file extension .scala just in case.
If your syntax is correct, there should appear a “run” button right above @main, you can click that too.

In worksheets (again, these end with .worksheet.sc), you can simply press Ctrl+S to save the file.
Every time the worksheet is saved, it is evaluated automatically.

In non-worksheets (these are .scala files), you can click “run” above @main. I’m not sure about the green triangle. I don’t think it works.

Unfortunatyely No
Please see the below image

It looks like you forgot to save the file.

1 Like

Thanks a lot

1 Like

Great! :smiley_cat:

I prefer worksheets, since they don’t require any “running”. I hope you can give that a try.
If you need more help in the future, come to Scala Discord since it’s much easier to have long conversation there.