学习 Scala 作为第一种虚拟机/编译语言 - 工作流程挑战

发布于 2024-10-11 08:12:20 字数 314 浏览 3 评论 0原文

我有 PHP/Python/Javascript 背景,最近对 Scala 非常感兴趣 - 特别是从 Web 角度来看 Akka。

不过,与我所描述的解释语言相比,我在一般工作流程方面遇到了非常困难的情况。

一般来说,我倾向于编码、测试结果、编码并重复。当即使更改 20 行类中的一行也需要长达 30 秒的编译和运行时间时,就会陷入停滞。这真的正常吗?我是否需要构建、构建、构建,然后 30 分钟或一个小时后返回并编译/测试?

(我正在将 IDEA 与 SBT 一起使用)除了链接到存储库之外,我是否需要专门学习如何使用 Maven?

想法?建议?

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/test?

(I'm using IDEA with SBT) Do I need to specifically learn how to use Maven other than linking to the repos?

Thoughts? Advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彩虹直至黑白 2024-10-18 08:12:20

我认为您的 Idea 和 SBT 走在正确的轨道上。您是否尝试过

~compile

这会自动检测您的源代码的更改。对于 Web 应用程序,您可以执行

jetty-run

以下操作

~prepare-webapp

来持续编译应用程序并将其重新部署到 jetty。让 Scala 开发感觉很像 Python Web 开发。

通常我发现 SBT 在编译时非常快,尤其是你所说的文件大小。当我保存更改并转到 SBT 提示时,一切就完成了。

另一个方便的 SBT 方面是 REPL,它将加载您的项目及其依赖项:

console

您可以使用

:replay

scala REPL 重新加载任何已编译的更改。

编辑:
我想我应该提到,您可以使用带有 main 方法的简单类。如果您创建一个名为 src/main/scala/Foo.scala 的文件,如下所示:

object Foo {
  def main(args: Array[String]) {
    println("Hello World")
  }
}

和一个文件 project/build/Build.scala 如下所示:

import sbt._
class Build(info: ProjectInfo) extends DefaultProject(info) {
  override def mainClass = Some("Foo")
}

然后在 sbt 提示符下,您可以执行

~run

来连续编译并运行 Foo。主要方法。您可能需要先在 sbt 中进行“重新加载”。从保存零钱到看到输出似乎需要2-3秒。然后您只需编辑、保存并查看更改即可。这是一个非常好的工作流程。

另外,不要忘记 REPL——绝对是学习 Scala 的重要工具。只需交互地玩它,您就可以学到很多东西。

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.

喜爱皱眉﹌ 2024-10-18 08:12:20

IDE 协助:

使用静态类型语言,我发现自己比使用动态类型语言所做的工作流程要少,但这只有在出色的 IDE 帮助下才可能实现(类型信息允许它尽早检测到错误,并在编辑时给出准确的建议),因此它确实在您描述的代码测试循环中节省了一些时间。

然而,IDEA 中的 Scala IDE 支持尚未达到 Java 级别,例如,
无论是在编辑时捕获错误(恕我直言)还是编译速度。

REPL/脚本支持:

不要忘记您仍然可以使用 Scala REPL,工作流程与您在 Python 中使用的工作流程非常相似。

IDEA + Scala速度:

可以参考这个问题更多关于IDEA+Scala速度的讨论。

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.

给妤﹃绝世温柔 2024-10-18 08:12:20

我将 JRebel 插件与 maven 一起使用。我关闭了 NetBeans 保存时编译功能,(不知道 intellij 是否有类似)并运行 scala:cc - 连续编译目标从控制台。它等待源代码中的任何更改,因此在进行一些更改后,文件将被编译,复制到 /target 目录,然后热交换到正在运行的虚拟机中。该过程需要以秒为单位,具体取决于文件的大小。(我假设您从事 Web 开发,因为您提到了 PHPJavaScript)有 fsc 服务器在后台运行,这也是编译速度加快的原因之一。
有一些小缺点,你不能更改超类,这意味着你不能从 AbstractFunction1AbstractFunction2 (代表匿名函数) - 更改 (x) =>; x(x,y) => x + y 表示需要重新启动服务器。
有用的链接:
scala:cc
jrebel

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

苯莒 2024-10-18 08:12:20

静态类型语言的优点之一是类型系统可以捕获多种类型的错误/错误。因此,从理论上讲,您不需要经常经历这些繁琐的事情。

当然,还有很多变化,尤其是 UI 方面,只有屏幕上的眼睛才能看到。我只能建议有良好的模块化来减少编译/构建时间。和单元测试。我不知道你来自哪个社区,但在 java/scala 中,强烈建议进行单元测试。这样你就能更快地发现代码是否正常工作。

答案可以归结为:尽可能避免构建和重新启动来检查您的工作。

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.

⊕婉儿 2024-10-18 08:12:20

我的类似于 Python 的工作流程(这让我几乎没有时间等待)通常如下所示:

  • 将我想要做的事情变成一个库(将其放入 .jar并将其添加到类路径中)。

  • 在编辑器中处理新代码,但将其粘贴到 REPL 中而不是单独编译。一旦运行良好,请添加单元测试。

  • 将新代码放入库中。重复。

我可以在上网本上执行此操作,并且任何一步的等待时间不会超过 3 秒。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文