Installing scala from coursier: where is the `scala` command? - scala

I installed scala from https://www.scala-lang.org/download/scala3.html using the coursier option:
cs install scala3-compiler
cs install scala3-repl
After these two steps, I have two binaries in my $PATH: scala3-compiler and scala3-repl.
I am learning scala from the Programming in Scala book, and in Chapter 4, it said I should compile and run a scala application with the following commands:
scalac ChecksumAccumulator.scala Summer.scala
scala Summer of love
I replaced scalac with scala3-compiler in the first command, and it produced an org directory of the following structure:
org/
└── stairwaybook
└── classesandobjects
├── ChecksumAccumulator$.class
├── ChecksumAccumulator.class
├── ChecksumAccumulator.tasty
├── Summer$.class
├── Summer.class
└── Summer.tasty
But I cannot find any scala command installed in my system, and do not know how to run the compiled bytecodes. Any thoughts or suggestions?

Try to install:
cs install scala3
Then you should write:
scala3-compiler ChecksumAccumulator.scala Summer.scala
scala3 Summer of love
It should work

Related

why should i create package for my own flutter project

I'm learning bloc state management in flutter but I saw code here made me to ask some questions like ,
why they separated the repository as an external package, then use it in the bloc? . why didn't just create repository auth in the project lib folder like this
├── android
├── ios
├── lib
│ └── authentication_repository
└── test
and what is the benefit of this approach when should I use it in my future projects?

How to run Scala 3 applications in the command line with Coursier

If you follow the steps at the official Scala 3 sites, like Dotty or Scala Lang then it recommends using Coursier to install Scala 3. The problem is that neither or these explain how to run a compiled Scala 3 application after following the steps.
Scala 2:
> cs install scala
> scalac HelloScala2.scala
> scala HelloScala2
Hello, Scala 2!
Scala 3:
> cs install scala3-compiler
> scala3-compiler HelloScala3.scala
Now how do you run the compiled application with Scala 3?
Currently there does not seem to be a way to launch a runner for Scala 3 using coursier, see this issue. As a workaround, you can install the binaries from the github release page. Scroll all the way down passed the contribution list to see the .zip file and download and unpack it to some local folder. Then put the unpacked bin directory on your path. After a restart you will get the scala command (and scalac etc) in terminal.
Another workaround is using the java runner directly with a classpath from coursier by this command:
java -cp $(cs fetch -p org.scala-lang:scala3-library_3:3.0.0):. myMain
Replace myMain with the name of your #main def function. If it is in a package myPack you need to say myPack.myMain (as usual).
Finally, it seems that is possible to run scala application like scala 2 version using scala3 in Coursier:
cs install scala3
Then, you can compile it with scala3-compiler and run with scala3:
scala3-compiler Main.scala
scala3 Main.scala
This work-around seems to work for me:
cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- YourScala3File.scala
This way, you don't even have to compile the source code first.
In case your source depends on third-party libraries, you can specify the dependencies like this:
cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- -classpath \
$(cs fetch --classpath io.circe:circe-generic_3:0.14.1):. \
YourScala3File.scala
This would be an example where you use the circe library that's compiled with Scala 3. You should be able to specify multiple third-party libraries with the fetch sub-command.

How to run Main method in Scala in Intellij

I am trying to get a main method to run in Scala in Intellij, but it is not working, and I don't know why.
File structure looks like this...
.
├── cse116.iml
├── pom.xml
├── src
│   ├── Main.scala
└── target
└── classes
Main.scala looks like this...
object Main {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
When I try to do "Run" on Intellij, I get the following error:
Error: Could not find or load main class Main
I am not sure what configuration is messed up. What may be some common reasons for this happening?
Try to right click on your mouse when you are in Main.scala file and select "run 'Main.main()'" option with green triangle
Assuming you are running a recent version of intelliJ, have the Scala plugin installed and your dependencies for Scala is correct in your pom file. You should see a tiny green arrow either on the left hand side of your main class or when right clicking within that class. Also, if you are on macOS there is the ctrl+shift+r to run the file as shown in the image below.
If that is not the case, consider creating a new Scala project from the IDE itself. You'd then choose an sbt project. sbt is the equivalent of maven in the Scala world. This one "should" work, if it doesn't it is very likely to give you a better Scala specific error message(s)

Can you put SBT code inside a scala script?

I'm sure I've seen a page that shows how to put SBT instructions inside a scala file, instead of build.sbt. The advantage is that everything then lives in a single file, which makes it easier to use Scala as a scripting language.
But I can't now find the example... can anyone help?
BTW, I know about ammonite, but it has limitations compared to SBT.
I think you're looking for this page of the sbt docs: Scripts, REPL, and Dependencies. There are also some other resources on the topic:
an example project by Eric Richardson with detailed instructions: ekrich/sbt-scripting
a post by Eugene Yokota (maintainer of sbt): scripting with Scala (it's from 2014, so I'm not sure if it's still relevant)
an example script gist by Seth Tisue
You can create your customize scala file under project/ directory and import it in build.sbt, like:
├── build.sbt
└── project
└── Settings.scala
so you can import it in build.sbt:
import Settings._

IntelliJ SBT directory structure

I created a new SBT project HelloScala, then I created a package called week6 and then a Scala worksheet under it. Eventually, I got the following directory structure:
~/HelloScala/.idea
~/HelloScala/project
~/HelloScala/src/main/scala
~/HelloScala/src/main/scala-2.12/week6/hello.sc
However, when I put package week6 in the worksheet hello.sc, IntelliJ warns that Project names doesn't correspond to directories structure. My questions are:
What should be the correct directory structure IntelliJ/SBT expects? Is it specified in some .xml file?
Notice that IntelliJ created two scala subdirectories, scala and scala-2.12. Is it correct? Or IntelliJ somehow failed to recognize the scala which had already existed but download and install another scala?
Thank you!
I don not use IntelliJ that often, but usually Scala packages should go into src/main/scala.
The structure of a Scala project is nothing too different from a Java one. Minimally you will need:
myproject
├── build.sbt
└── src
└── main
└── scala
└── mypackage
which is a project with a single package, a SBT script and nothing else.
For a more complex example, a Scala project could look like:
project
├── build.sbt
├── project
├── target
└── src
├── main
│ ├── java
│ ├── resource
│ └── scala
└── test
├── java
├── resource
└── scala
in which the project folder is used by SBT for various purposes, the target folder contains the compiled .class files and .jar packages.
To answer your questions:
The above structure is used implicitly by the SBT plugin. Maybe it is customizable, but in most cases it is suggested against doing this.
Your Scala source code should go into src/main/scala. I have no idea where does the other directory come from.
#sgu It seems I misunderstood your question and unfortunately I cannot leave a comment. The file "hello.sc" you are trying to deal with is treated by IntelliJ as a "Scala worksheet". I guess it is a REPL behind IntelliJ which evaluates as you edit. However it is not treated the same as a "Scala source file", so adding package xxx gives you the warning. If you want to create a package, the source files should be in ".scala" extension.
You should put all your source code under "main/scala" directory. "Scala-2.12" is the directory, that would be created with sbt under "project/target" after you compile/build it.
Have you tried clicking the Make project option on top of the scala sheet in Intellij Idea? I use that option when I need to import oher packages into one scala worksheet.