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 ?
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.
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.
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.
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.
It imports the methods and variables of the a object. So if you want to call a.foo()
, you can just call foo()
instead.
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