Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this import exactly mean in Scala?

I encountered the following in Scala code:

class MyClass {
  ...
  val a = new A; import a._

}

What does exactly val a = new A; import a._ mean ?

like image 675
Michael Avatar asked May 10 '12 15:05

Michael


People also ask

What does import mean 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.

What are packages in Scala?

Package in Scala is a mechanism to encapsulate a group of classes, sub packages, traits and package objects. It basically provides namespace to put our code in a different files and directories. Packages is a easy way to maintain our code which prevents naming conflicts of members of different packages.

How do you define a function in Scala?

In scala, functions are first class values. You can store function value, pass function as an argument and return function as a value from other function. You can create function by using def keyword. You must mention return type of parameters while defining function and return type of a function is optional.

What is underscore in Scala?

Overview. The underscore (_) is one of the symbols we widely use in Scala. It's sometimes called syntactic sugar since it makes the code pretty simple and shorter. But, this often results in a lot of confusion and increases the learning the curve.


1 Answers

It imports the methods and variables of the a object. So if you want to call a.foo(), you can just call foo() instead.

like image 58
sepp2k Avatar answered Sep 18 '22 10:09

sepp2k