Learning Scala as a first VM/Compiled language - Workflow challenges
I'm coming from a PHP/Python/Javascript background, and recently became very interested in Scala - specifically Akka coming from the web standpoint.
I'm having an extremely hard time though with general workflow, issues compared to interpreted languages such as the ones I described.
In general I tend to code, test results, code and repeat. This comes to a standstill when even changing a single line in a 20 line class takes up to 30secs to compile and run. Is this really normal? Do I need to just build, build, build then go back 30 minutes or an hour later and compile/tes开发者_高级运维t?
(I'm using IDEA with SBT) Do I need to specifically learn how to use Maven other than linking to the repos?
Thoughts? Advice?
I think you're on the right track with Idea and SBT. Have you tried
~compile
That will detect changes to your source automatically. For web applications, you can do a
jetty-run
followed by
~prepare-webapp
To continuously compile and redeploy your app to jetty. Makes Scala dev feel a lot like Python web development.
Usually I've found SBT to be very fast when compiling, especially the size file you're talking about. By the time I save my change and go to my SBT prompt, it's done.
Another handy SBT aspect is the REPL which will load your project and its dependencies:
console
You can reload any compiled changes with
:replay
in the scala REPL.
EDIT: Guess I should mention that you can play around with a simple class with a main method. If you create a file called src/main/scala/Foo.scala that looks like this:
object Foo {
def main(args: Array[String]) {
println("Hello World")
}
}
And a file project/build/Build.scala like this:
import sbt._
class Build(info: ProjectInfo) extends DefaultProject(info) {
override def mainClass = Some("Foo")
}
Then at the sbt prompt, you can do
~run
To continuously compile and run the Foo.main method. You may need to do a 'reload' in sbt first. It seemed to take 2-3 seconds from saving change to seeing output. Then you just edit away, save and see changes. It's a pretty good workflow.
Also, don't forget the REPL - definitely a critical tool for learning Scala. You can learn a ton just playing with it interactively.
IDE Assistance:
With static typing language I find myself doing less of that workflow than I do with dynamic typing, but that was only possible because of excellent IDE assistance (the typing information allows it to detect errors early, and give accurate suggestions while you are editing), so it does save some time in that code-test loop you described.
However Scala IDE support in IDEA isn't yet at the level of Java for example, both in catching errors while editing (IMHO) and speed of compilation.
REPL/Script support:
Do not forget that you can still use the Scala REPL, the workflow is pretty much like what you would be used to in Python for example.
IDEA + Scala speed :
You can refer to this question for more discussion on IDEA+Scala speed.
I use JRebel
plugin with maven
. I turn off the NetBeans
compile on save feature,(don't know if intellij
has similar) and run scala:cc
- continuous compilation goal from console. It waits for any changes in source code, so after you make some, the file gets compiled, copied to /target
directory and then hotswapped into running virtual machine. The procedure takes units of seconds depending on the size of the file.(I assume you do web development since you mentioned PHP
and JavaScript
) There is fsc
server running in the background, that is also one of the reasons, why the compiling is speeded up.
There are some minor disadvantages, you can't change the superclass, which means you can't go from AbstractFunction1
to AbstractFunction2
(which represent anonymous functions) - changing (x) => x
to (x,y) => x + y
means that you need to restart the server.
Useful links:
scala:cc
jrebel
One of the advantages to statically typed languages is that the type system can catch several types of mistakes/bugs. So, in theory, you shouldn't need to go through the rigmarole quite so often.
Of course there are many changes, especially in the UI, that only eyeballs on screen can check. All I can suggest there is good modularization to keep the compile/build time down. And unit tests. I don't know about the community that you're coming from, but in java/scala, unit tests are highly, highly recommended. You find out if the code worked right much faster that way.
The answer boils down to this: try and avoid having to build and restart to check your work as much as possible.
My Python-like workflow--which leaves me with very little time waiting--usually goes as follows:
Turn what I'm trying to do into a library (stick it in a
.jar
and add it to the classpath).Work on the new code in an editor, but paste it into the REPL instead of compiling separately. Once it's working well, add a unit test.
Place the new code into the library. Repeat.
I can do this on a netbook and not wait longer than ~3 seconds for any one step.
精彩评论