Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing something like Python's "import" in Scala

Is it possible to use Scala's import without specifying a main function in an object, and without using the package keyword in the source file with the code you wish to import?

Some explanation: In Python, I can define some functions in some file "Lib.py", write

from Lib import *

in some other file "Run.py" in the same directory, use the functions from Lib in Run, and then run Run with the command python Run.py. This workflow is ideal for small scripts that I might write in an hour.

In Scala, it appears that if I want to include functions from another file, I need to start wrapping things in superfluous objects. I would rather not do this.

like image 521
emchristiansen Avatar asked Nov 04 '22 22:11

emchristiansen


1 Answers

Writing Python in Scala is unlikely to yield satisfactory results. Objects are not "superfluous" -- it's your program that is not written in an object oriented way.

First, methods must be inside objects. You can place them inside a package object, and they'll then be visible to anything else that is inside the package of the same name.

Second, if one considers solely objects and classes, then all package-less objects and classes whose class files are present in the classpath, or whose scala files are compiled together, will be visible to each other.

like image 172
Daniel C. Sobral Avatar answered Nov 15 '22 05:11

Daniel C. Sobral