0

I'm working on an exercise. I need to create a java project that can be run from the scala command line. The final output should be this:

scala> int2str(6)
res0: String = six

scala> int2str(65)
res0: String = sixty-five

How do I create a function that can be accessed by scala like that? I can create a Scala project in IntelliJ, but I don't know how to export that function to be used like that.

Any help is appreciated.

Thanks

  • Why don't you define the function just in Scala console? – Bondarenko Aug 20 '21 at 19:21
  • 1
    You don't need to "export" anything. You want the REPL to import or load the function. How to do that depends somewhat on what form the function is in. Is it compiled in a `.jar` file? In any event, try `scala -help` on your shell command line and read about the various options. – jwvh Aug 20 '21 at 19:54

1 Answers1

0

Assuming you have the following scala code:

object A {
 def int2str(i: Int): String = ""
}

You can just make a jar file from your scala project with a command sbt package for instance, then run scala -cp <your jar>. In the scala console just import your function:

import A.int2str
Bondarenko
  • 225
  • 1
  • 7