object field import in scala - scala

I have a very simple question of style.
What is the best way to access a field or a method defined in an object ?
For example if I have an object Foo that define a field bar, would you rather do import Foob.bar and then refer to it in the scope as bar or call Foo.bar wherever you need the field. From what I have read in most libraries, Foo.bar seems the standard way to do it but I would like to be sure.
Thanks,

Yeah, usually Foo.bar is used, but that depends on the coder community behind the project as Frank pinted out.
If you would like to use all members without having to prefix it with Foo, you can do that too:
object Foo {
val x = "..."
}
class Foo {
import Foo._
println(x)
}

Related

How could a "global implicit class" be defined in Scala?

Considering that a implicit class "must be defined inside of another trait/class/object"1, how can a implicit conversion be defined globally?
The case is that I'd like to add a method to all Strings (or Lists) in my application, or at least to several packages of it.
One cannot add anything to the "global" scope, neither in Java, nor in Scala.
However, in Scala one can define package objects, which can contain methods that are used all over the package, and can be easily imported by the user.
This looks something like this: in the directory foo/bar/baz one creates a file called package.scala with the following content:
package foo.bar
package object baz {
implicit def incrediblyUsefulConversion(s: String) = ...
}
The user then can do the following in his code to activate the conversion:
import foo.bar.baz._
or maybe
import foo.bar.baz.incrediblyUsefulConversion
Of course, you can also use your own code in other packages, just like any other user.

How to (properly) enrich the standard library?

I would like to define an implicit conversion from Iterator[T] to a class that I have defined: ProactiveIterator[A].
The question isn't really how to do it but how to do it properly, i.e. where to place the method, so that it is as transparent and unobtrusive as possible. Ideally it should be as the implicit conversion from String to StringOps in scala.Predef If the conversion was from a class in the library to some other class, then it could be defined inside that class, but AFAIK that's not possible here.
So far I have considered to add an object containing these conversions, similarly to JavaConversions, but better options may be possible.
You don't really have much of a choice. All implicits must be contained within some sort of object, and imported with a wildcard import (you could import them individually, but I doubt you want that).
So you'll have some sort of implicits object:
package foo.bar
object Implicits {
implicit class ProactiveIterator[A](i: Iterator[A]) {
...
}
}
Then you must explicitly import it wherever you use it:
import foo.bar.Implicits._
In my opinion, this is a good thing. Someone reading the code might not understand where your pimped methods are coming from, so the explicit import is very helpful.
You can similarly place your implicits within a package object. You would have to import them the same way into other namespaces, but they would be available to classes within the same package.
For example, using the following, anything within foo.bar will have this implicit class available:
package foo
package object bar {
implicit class ProactiveIterator[A](i: Iterator[A]) {
...
}
}
Elsewhere you would import foo.bar._ (which may or may not be as clean, depending on what's in bar).

How to define a global function in scala?

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(...)
}
}

Import package as another

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.

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 ?
It imports the methods and variables of the a object. So if you want to call a.foo(), you can just call foo() instead.
It means that all methods and variables of a object of A type are now available in this block (scope) without explicitly mentioning a. So if A has a bar() method you can now say:
bar()
instead of
a.bar()
but only within the scope where import is defined.
Let's explain this with something you should be familiar with:
println("Hello world")
The question is: why does that work? There's no object called println with an apply method, which is the usual explanation for code that looks like that. Well, as it happens, the above code is really doing this:
Predef.println("Hello world")
In other words, println is a method on the object scala.Predef. So, how can you use it like above? Well, like this:
import scala.Predef._
println("Hello world")
Importing the contents of a stable reference (ie, not a var or a def) will make its methods available without having to prefix them with reference..
It also makes any implicits defined inside it available, which is how the implicit conversions defined inside scala.Predef are made available as well -- Scala imports the contents of java.lang, scala and scala.Predef (in that order, so the latter ones override the earlier ones).