Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is scala import recursive?

Tags:

import

scala

With

import mypack._

do I still need to

import mypack.box.writer
import mypack.box.reader

and

import mypack.box.parser.stringparser

?

And what's the proper keyword to search/google? "Recursive" gives me overwhelming "tail recursion" results.

like image 574
galaapples Avatar asked Oct 23 '13 21:10

galaapples


People also ask

What is import in Scala?

Importation in Scala is a mechanism that enables more direct reference of different entities such as packages, classes, objects, instances, fields and methods. The concept of importing the code is more flexible in scala than Java or C++. The import statements can be anywhere.

Does Scala have tail recursion?

Recursion is a method which breaks the problem into smaller subproblems and calls itself for each of the problems. That is, it simply means function calling itself. The tail recursive functions better than non tail recursive functions because tail-recursion can be optimized by compiler.

What is recursion in Scala?

Advertisements. Recursion plays a big role in pure functional programming and Scala supports recursion functions very well. Recursion means a function can call itself repeatedly. Try the following program, it is a good example of recursion where factorials of the passed number are calculated.

What is tailrec in Scala?

The @tailrec annotation needs to be added to the method. This tells the compiler to verify the code has been compiled with tail call optimization. The last call of the method must be the recursive one.


1 Answers

No, Scala import is not recursive.

Packages are there to keep the namespace in the current scope clean. Importing all subpackages by default would go against that.

On the other hand, imports are relative, so you can do this:

import mypack._
import box.writer
import box.reader
import box.parser.stringparser

Some people dislike this style, as it is somewhat error-prone. I dislike it because there's no clear distinction between absolute and relative imports. Still, it helps sometimes.

like image 180
Daniel C. Sobral Avatar answered Sep 28 '22 04:09

Daniel C. Sobral