I'm a new in Scala. I created a package object in my code:
package mypackage.spark
import scala.language.implicitConversions
import org.apache.spark.SparkContext
import mypackage.spark.SparkContextFunctions
package object spark {
implicit def toSparkContextFunctions(sc: SparkContext): SparkContextFunctions =
new SparkContextFunctions(sc)
}
I expect that when I use import mypackage.spark._, I will able to use methods from SparkContextFunctions class. This approach works for me, when I use only only one imported package object. But when I add additional import in my code. For example:
import mypackage.spark._
import com.datastax.spark.connector._
com.datastax.spark.connector._ doing the same for org.apache.spark.SparkContext class. My code stop compile and I have an error that used method is not a member of SparkContext class. When I change the order of imports the compiler starts see methods from mypackage.spark._ and stops see methods from com.datastax.spark.connector._
Maybe I missed something? Or Scala doesn't support this?
Thanks.
If you need to use two classes named SparkContext at the same time, you can alias them:
import my.package.name.{SparkContext => MySparkContext}
import some.other.package.name.{SparkContext => OtherSparkContext}
Classes from the same package you can be aliased in the same import:
import my.package.name.{SparkContext => MySparkContext, SomethingElse => MySomethingElse}
You may want to choose better names than MyXXX and OtherXXX.
The imports may conflict in two ways: either both use toSparkContextFunctions for the implicit conversion name or both provide extension methods with the same name (even if different signature).
If neither is the case, there should be no problem. If one is, change your method names, since you can't change the ones in com.datastax.spark.connector.
Related
I'm learning java and I'm told this package is provided by default, to every class, because its methods are so common. I thought I would try to import it, any way to see what would happen. I know its not practical and probably expensive but I'm curious as to why it's doesn't work from a technical point of view.
import javax.lang.*;//why doesn't this work.
javax.lang contains only a single package: model
https://docs.oracle.com/javase/7/docs/api/index.html?javax/lang/model/package-summary.html
you're not doing anything by importing this package. Maybe you're confusing it with java.lang ?
You don't need to import java.lang.*
There is one exception to the import rule. All classes in the java.lang package are imported by default. Thus you do not need to import java.lang.*; to use them without fully qualified names.
Consider the System.out.println() method we've been using since the first day of class.
System is really the java.lang.System class. This class has a public static field called out which is an instance of the java.io.PrintStream class. So when you write System.out.println(), you're really calling the println() method of the out field of the java.lang.System class.
I want to use an immutable Queue in Scala, like this:
var a:Queue[Int] = Queue.empty[Int]
However, I get the following error:
error: not found: type Queue
I tried importing the library containing it but there was no effect:
import scala.collection.immutable
Pretty sure you need to add ._ to your import, like so:
import scala.collection.immutable._
Or import Queue specifically as:
import scala.collection.immutable.Queue
I'm using play framework, I want to define a global function, How can I do it?
First I define the function in SomeFunc.scala and import it to every file which I will use it.
Is it possible to direct use it like println without import SomeFunc.scala
println is defined in the object scala.Predef. The members of which is always in scope, there is no way you can add to that, but as the question linked to by senia says you can achieve sort of the same by defining a method in a package object which will then be available inside code in that package.
Another solution that some libraries uses is to provide an Imports object with aliases and shortcuts just like Predef, but that you have to explicitly import with a wildcard. For example nscala-time does this:
import com.github.nscala_time.time.Implicits._
Yes it is possible, but only global in the same package, not absolute global.
package com
package object myproject {
def myGlobalFunc(..) = ...
}
Then you use it like this:
package com.myproject
object HelloWorld {
def main(args: Array[String]) {
myGlobalFunc(...)
}
}
Everything is in the title : How do I do for import a trait in a worksheet for testing it? I have a function in that trait that I want to test..
Just import it like you always would:
import com.mytrait.MyTrait
Let's say I have a Scala project with a bunch of case classes under package com.example.a.b.c. I want to import all these classes into a package com.example.c (which contains a few more non-conflicting case classes) so that anywhere else in my project, I only need to import com.example.c._ to use every case class both from com.example.c and com.example.a.b.c.
How can I do that?
There is discussion of adding an export mechanism which would do what you want, but it's not clear whether that will happen.
In any case, for now the only way is to
Define the type of every class
Set a val equal to every object
So for example,
package bar
case class Foo(i: Int) {}
would need to be mimicked in another package with
package object baz {
type Foo = bar.Foo
val Foo = bar.Foo
}
When faced with this, people usually just settle for an extra import or two.
The import statement in scala just tells the compiler where to find other classes like in java, not like the #include directive in C/C++ where the compiler physically copies the entire header file. If you want to use the case classes from com.example.a.b.c, you should import them from their own package as this is the conventional way.