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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With