New to the whole Scala ecosystem, I've been reading various open
source projects and so on but can't seem to pinpoint a convention or
community practice for this specific question I have... What is the
best practice for import statements?
I realize this question is vague, so more specifically:
* Try to avoid wildcard ('_') imports as much as possible?
* If only one method uses some identifier heavily, is it looked down
upon to put the import at the top of the method definition? Should I
put it outside? etc.
I just want to write code that is acceptable by the community
standards, so I figured I'd ask here.
Thanks,
Mitchell
-Stefam
2011/3/4 Lex <lex...@gmail.com>:
Problem is that most IDEs for scalas do not handle the import stuff
too well. One thing I find important for imports is to keep their
scope as tight as possible to prevent accidental implicit inclusion.
I'm also a fan of always specifiing the complete import although I'm
being lazy on that lately and use the short time more often.-Stefam
You will have to use wildcards _ to pull in the implicits. Other than that, just use common sense.
On Thu, Mar 3, 2011 at 8:08 PM, Mitchell Hashimoto <mitchell....@gmail.com> wrote:
Hello,
New to the whole Scala ecosystem, I've been reading various open
source projects and so on but can't seem to pinpoint a convention or
community practice for this specific question I have... What is the
best practice for import statements?
Hope that helps.
We follow the GWT coding standard on ordering imports:
http://code.google.com/webtoolkit/makinggwtbetter.html#codestyle
For the collection classes, we do
import scala.collection.{immutable,
mutable}
and then refer to any classes in there using mutable.HashMap.
As others have stated, we don't use wildcard imports.
Blair
On Tue, Mar 8, 2011 at 4:00 PM, Brian Maso <br...@blumenfeld-maso.com> wrote:
> Dude, you sound like a corporate Java programmer. Best practices? We don't
> need no stinkin' best practices!
Heh, I take that offensively! ;)
I wasn't asking for hard and fast rules, just ways to write idiomatic
Scala so that if others view my code they won't be like "What was this
guy thinking?"
Nothing too enterprisey ;)
Best,
Mitchell
Maybe look at a number of different Scala projects, and try to see if there is a trend...
And maybe the Scala style guide (which tries to capture best practices in Scala) has a
word on imports (I don't recall...).
>>> * Try to avoid wildcard ('_') imports as much as possible?
It depends... It is nearly unavoidable when you import the methods. It might be better to
use _ instead of a very long list of classes...
Although I used an explicit list of imports in a series of tutorials, as it might have an
educational purpose (showing where the classes come from in a complex set of Java packages):
import java.net.URL
import org.apache.pivot
import pivot.beans.Bindable, pivot.util.Resources,
pivot.util.concurrent.TaskExecutionException
import pivot.collections.{ Map, Sequence }
import pivot.wtk.
{
Label,
ListView,
ListViewSelectionListener,
Span,
Window
}
At least, it is less ugly than in Java... :-)
>>> * If only one method uses some identifier heavily, is it looked down
>>> upon to put the import at the top of the method definition? Should I
>>> put it outside? etc.
As Brian said, it is a good idea to put the import inside the method, to limit the scope
of the imported classes. Another nice things with Scala imports, following best practice
to declare variables near the point where they are used.
I also saw stuff like defining an 'object', and immediately after importing its content to
be used directly in the companion class (as regular 'static' items).
object TT
{
val Message = "Foo"
def main(args: Array[String])
{
val tt = new TT
tt.doStuff
}
def output(s: String) { println("Message: " + s) }
}
import TT._
class TT
{
def doStuff() { output(Message) }
}
(Instead of def doStuff() { TT.output(TT.Message) })
(Not sure why they have visibility on their private parts but no direct access on the
elements.)
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
Thanks for your notes. I'm an emacs user so "IDE" support is not an issue, I prefer to do that manually.