Play 2.0+Java 与 Play 2.0+Scala?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12552210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play 2.0+Java vs. Play 2.0+Scala?
提问by dreampowder
I am thinking about migrating to play 2.0 after play 1.2. One thing that bothers me is that people say Scala is more "preferred" for a play 2.0 application. I know the differences over 1.2 and 2.0, but I'm unsure if there are differences between Play 2.0 with Java and Play 2.0 with Scala
我正在考虑在玩 1.2 之后迁移到玩 2.0。困扰我的一件事是人们说 Scala 更“首选”用于 play 2.0 应用程序。我知道 1.2 和 2.0 之间的差异,但我不确定 Play 2.0 with Java 和 Play 2.0 with Scala 之间是否存在差异
So there are questions in my mind:
所以我心里有几个问题:
- Is there anything that I cannot do with java over scala in a Play 2.0 application?
- What advantages do I have if I start to learn and use scala in a play 2.0 application?
- 在 Play 2.0 应用程序中,有什么我不能通过 Scala 使用 java 做的事情吗?
- 如果我开始在play 2.0应用程序中学习和使用scala,我有什么优势?
采纳答案by kapex
I just finished a prototype using Play 2.0 with Java and now am considering to learn Scala just so I can switch to it for further development.
我刚刚使用 Play 2.0 和 Java 完成了一个原型,现在正在考虑学习 Scala,这样我就可以切换到它进行进一步的开发。
It's not just the usual Java vs. Scaladiscussion - the problem as I see it with the Play framework is that it forces Scala idioms onto Java. An example from the documentation about calling multiple web services:
这不仅仅是通常的Java 与 Scala 的讨论——我在 Play 框架中看到的问题是它强制将 Scala 习语用于 Java。文档中有关调用多个 Web 服务的示例:
public static Result feedComments(String feedUrl) {
return async(
WS.url(feedUrl).get().flatMap(
new Function<WS.Response, Promise<Result>>() {
public Promise<Result> apply(WS.Response response) {
return WS.url(response.asJson().findPath("commentsUrl").get().map(
new Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
return ok("Number of comments: " + response.asJson().findPath("count"));
}
}
);
}
}
)
);
}
It works but doesn't look like conventional Java. Those parentheses look really scary. Even Eclipse gets confused and never knows what generics I need or want to use - I always have to choose them manually.
它可以工作,但看起来不像传统的 Java。那些括号看起来真的很可怕。甚至 Eclipse 也会感到困惑,永远不知道我需要或想要使用哪些泛型——我总是不得不手动选择它们。
Also note that in the documentation they made this look pretty by removing @Overrideannotations, using just two spaces for indentation and overall choosing a very simple example without validation or error recovery so they don't use too many lines. I'm not even sure you could configure a code formatter to output it like this without messing up other code completely.
另请注意,在文档中,他们通过删除@Override注释,仅使用两个空格进行缩进并总体选择一个非常简单的示例而无需验证或错误恢复,从而使这看起来很漂亮,因此他们不会使用太多行。我什至不确定您是否可以配置代码格式化程序来像这样输出它而不会完全弄乱其他代码。
In practice I ended up with an unreadable block of a horrible Java-Scala-abomination just for getting some data from another service.
在实践中,我最终得到了一个可怕的 Java-Scala-abomination 的不可读块,只是为了从另一个服务获取一些数据。
Unfortunately I can't find any example of combining responses in Scala. At least calling single web services in Scalalooks much shorter and easier to read.
不幸的是,我找不到任何在 Scala 中组合响应的示例。至少在 Scala 中调用单个Web 服务看起来更短且更易于阅读。
回答by ndeverge
AFAIK, it is better to go with Scala, because it offers more functionnality.
AFAIK,最好使用 Scala,因为它提供了更多功能。
For instance (correct me if I'm wrong), you can directly using the Iterateeswith Scala (aka reactive programming), but I don't know how to achieve that with Java.
例如(如果我错了,请纠正我),您可以直接将Iteratees与 Scala(又名反应式编程)一起使用,但我不知道如何使用 Java 实现这一点。
Also, the Java API of Play is an upper layer in the Play architecture, the core is written with Scala; so if you want to lean what's happening inside, go with Scala.
同时,Play的Java API是Play架构的上层,核心是用Scala编写的;因此,如果您想了解内部发生的事情,请使用 Scala。
And also note that you can mix Java and Scala in the same Play project, so you can move smoothly to Scala.
还要注意,您可以在同一个 Play 项目中混合使用 Java 和 Scala,因此您可以顺利迁移到 Scala。
回答by sleeplessnerd
I was new to Play and just wrote a small-mid sized Project with the Java side of it.
我是 Play 的新手,刚刚用 Java 编写了一个中小型项目。
It works and is powerful, but a lot of Features just seem to be very Scala centric and the Java support is sub-par. The Java API Doc is virtually non existent, which takes away some comfort in the IDE (IntelliJ - I recommend it). The integration in the Java world is not very "natural" (SBT..), It does not build as a war or maven module (but there seem to be plugins to do this). You kind of loose the benefits of the existing Java tooling.
它可以工作并且功能强大,但是很多功能似乎都非常以 Scala 为中心,并且 Java 支持低于标准。Java API Doc 几乎不存在,这在 IDE 中带来了一些安慰(IntelliJ - 我推荐它)。Java 世界中的集成不是很“自然”(SBT ..),它不是作为 war 或 maven 模块构建的(但似乎有插件可以做到这一点)。您有点失去了现有 Java 工具的好处。
The concepts which differentiate Play from the other Frameworks almost all seem to be scala native and Java second.
将 Play 与其他框架区分开来的概念几乎都是 Scala 原生的,其次是 Java。
I stumbled onto some bugs and quirks and I always would end up in Scala code I had to read and understand to fix or work around it. (Actions, RequestBody stuff, Async, routes, template implicits and so on)
我偶然发现了一些错误和怪癖,我总是以 Scala 代码告终,我必须阅读和理解才能修复或解决它。(动作、RequestBody 东西、异步、路由、模板隐式等)
So the Java side of things just feels very second tier to me, although they are clearly making an effort for them to be equally good.
因此,Java 方面对我来说只是感觉非常次要,尽管他们显然正在努力使它们同样出色。
Now, after the Project, I would say it is very usable and would even use it again (in Java).
现在,在项目之后,我会说它非常有用,甚至会再次使用它(在 Java 中)。
回答by GreenAsJade
What advantages do I have if I start to learn and use scala in a play 2.0 application?
如果我开始在play 2.0应用程序中学习和使用scala,我有什么优势?
As djangofan commented, when you google for Play help, you get examples in Scala.
正如 djangofan 评论的那样,当您在 google 上搜索 Play 帮助时,您会在 Scala 中获得示例。
Is there anything that I cannot do with java over scala in a Play 2.0 application?
在 Play 2.0 应用程序中,有什么我不能通过 Scala 使用 java 做的事情吗?
However, I haven't yet (still a beginner) found an equivalent of Ebean for Scala. The Scala examples I've found all use direct SQL for persistence. Where's the database abastraction?
但是,我还没有(仍然是初学者)为 Scala 找到等效的 Ebean。我发现的 Scala 示例都使用直接 SQL 进行持久化。数据库抽象在哪里?
Since I haven't found a database abstraction for Play with Scala, I'm continuing with Java...
由于我还没有找到 Play with Scala 的数据库抽象,我将继续使用 Java...

