Can I execute a scala code file as a part of my code? - scala

I woud like my application to execute external scripts written in a Scala-based DSL i've developed. That's why I need something like this to work:
...
val a = evaluate("~/myextcode.scala")
...
myextcode.scala:
return 123
Can I reach some thing like this in Scala?

You can do that with Eval from twitter util. Check its Scaladocs here.

Related

How to execute scala tests programmatically

I'm looking for a way to execute scala tests (implemented in munit, but it could be also ScalaTest) programmatically. I want to perform more or less what sbt test does out-of-the box inside my own scala code, without running sbt (focusing on test discovery and execution and getting back a report).
I some something like this in mind:
object Test extends App {
val tests = TestDiscovery.discover("package.that.has.tests")
val reports = tests.foreach(test => test.execute())
// do something with the reports, maybe print to console
}
Is there any documentation related to this?
Scala Test has execute() and run().
In order to understand the impact of all the args it's worth looking at the Scala Test shell as well

Is there a way to run pure scala code in a script tag

Using the scalatags script tag I know I can define the following:
script("console.log('Running javascript code')")
But is there a way I can rather pass pure scala code instead? So something like the following:
script(println("Running scala code now"))
No, that is not possible.
You can achieve the same effect in a different way, by exporting a top-level function with the Scala code you want to run, then generate a script that calls that function:
object Exports {
#JSExportTopLevel("dynamicScriptCode")
def code(): Unit =
println("Running Scala code now")
}
...
script("dynamicScriptCode();")

Is it possible in Scala to have functions/methods inside a package{}?

I am currently following Miss Wiem Zine's article (specifically on ZIO): Make your program testable
but I couldn't get pass the
package console {
def println(line: String): ZIO[Console, Nothing, Unit] =
ZIO.accessM(_.console println line)
val readLine: ZIO[Console, Nothing, String] =
ZIO.accessM(_.console.readLine)
}
I've read in alvin alexander's page, but it's all about scoping.
I tried it in intellij, but it complains.
Could it be a
package object console {
...
}
instead of package console {}?
Or is that a scala 3 thing? Or am I doing something wrong?
In Scala 2, we have a concept of package objects. There are examples here: https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html.
I think code snippet from "Wiem Zine's article" is missing object keyword, so you are right it should be: package object console, since the whole example is on ZIO and Scala 2.x. There is no ZIO for Scala 3 yet, as far as I know :-)
#Thilo is also right that Scala 3 allows us to use top-level definitions right in the file without necessity to wrap into a block. Until Scala 3, one can use package object to get common package code.

Is it possible to execute string as code in scala

So for spark sql you have do something like this:
val query = s"select *from table...."
val temp = sqlContext.sql(query)
Is there anything similar to this for just regular spark code? Like in python they have this exec(string) function that take string and run it as code.
This is the link: Click Me
Gatling application is doing a similar trick, they allow you to write an arbitrary Scala code for load testing and then run it with their framework.
As far as I remember they basically write Scala code into a file, prepend/append it with something like:
class MyClass {
def foo() = {
// Code goes here
}
}
and execute Scala compiler to compile this code and then load this code as a regular class.

The difference between scala script and application

What is the difference between a scala script and scala application? Please provide an example
The book I am reading says that a script must always end in a result expression whereas the application ends in a definition. Unfortunately no clear example is shown.
Please help clarify this for me
I think that what the author means is that a regular scala file needs to define a class or an object in order to work/be useful, you can't use top-level expressions (because the entry-points to a compiled file are pre-defined). For example:
println("foo")
object Bar {
// Some code
}
The println statement is invalid in the top-level of a .scala file, because the only logical interpretation would be to run it at compile time, which doesn't really make sense.
Scala scripts in contrast can contain expressions on the top-level, because those are executed when the script is run, which makes sense again. If a Scala script file only contains definitions on the other hand, it would be useless as well, because the script wouldn't know what to do with the definitions. If you'd use the definitions in some way, however, that'd be okay again, e.g.:
object Foo {
def bar = "test"
}
println(Foo.bar)
The latter is valid as a scala script, because the last statement is an expression using the previous definition, but not a definition itself.
Comparison
Features of scripts:
Like applications, scripts get compiled before running. Actually, the compiler translates scripts to applications before compiling, as shown below.
No need to run the compiler yourself - scala does it for you when you run your script.
Feeling is very similar to script languages like bash, python, or ruby - you directly see the results of your edits, and get a very quick debug cycle.
You don't need to provide a main method, as the compiler will add one for you.
Scala scripts tend to be useful for smaller tasks that can be implemented in a single file.
Scala applications on the other hand, are much better suited when your projects start to grow more complex. They allow to split tasks into different files and namespaces, which is important for maintaining clarity.
Example
If you write the following script:
#!/usr/bin/env scala
println("foo")
Scala 2.11.1 compiler will pretend (source on github) you had written:
object Main {
def main(args: Array[String]): Unit =
new AnyRef {
println("foo")
}
}
Well, I always thought this is a Scala script:
$ cat script
#!/usr/bin/scala
!#
println("Hello, World!")
Running with simple:
$ ./script
An application on the other hand has to be compiled to .class and executed explicitly using java runtime.