[英]Is lack of 3rd party libraries preventing you using Scala?
I started learning Scala the other day. 我前几天开始学习Scala。 As for the language itself, I think it's fantastic, no problems there at all.
至于语言本身,我认为这很棒,没有任何问题。 To aid in my learning process I set myself a task, to download, parse and index text from HTML pages.
为了帮助我学习过程,我为自己设置了一个任务,从HTML页面下载,解析和索引文本。
In doing the above I found myself constantly digging into existing Java libraries. 在执行上述操作时,我发现自己不断深入研究现有的Java库。 I found that I had to use Java libraries to:
我发现我必须使用Java库来:
1) Open a connection - java.net.URL 1)打开连接 - java.net.URL
2) Parse the HTML (TagSoup - because a normal XML parser will not handle most badly formed HTML) 2)解析HTML(TagSoup - 因为普通的XML解析器不能处理最糟糕的HTML)
3) Index the text (Lucene) 3)索引文本(Lucene)
Given that I had to rely on Java libraries to do a quite a lot of the heavy lifting I was left wondering if it was worth me using Scala to begin with, other than as a learning exercise. 鉴于我不得不依靠Java库来完成相当多的繁重工作,我不知道是否值得我使用Scala开始,除了作为一个学习练习。 This was partly due to the fact that some extra mental effort was required to map between the two, for example, it's not intuitively obvious what the Scala type of a byte[] is, as everything is an object in Scala.
这部分是由于需要一些额外的心理努力才能在两者之间进行映射,例如,字体[]的Scala类型不是直观明显的,因为Scala中的所有东西都是对象。 It's this additional mental processing that can make the process seem a little clunky.
这是额外的心理处理,可以使这个过程看起来有点笨重。
Does anyone else think fewer 3rd party libraries (compared to Java) is a barrier to using Scala in commercial projects? 是否有人认为较少的第三方库(与Java相比)是在商业项目中使用Scala的障碍?
if you can call existing Java libraries does it even matter, or does having to span two different languages in a codebase make it harder? 如果你可以调用现有的Java库,那么它是否重要,或者在代码库中不得不跨越两种不同的语言会让它变得更难?
I don't quite see your concern. 我不太关心你的看法。 A Java library is typically a .jar file (compressed set of .class files).
Java库通常是.jar文件(压缩的.class文件集)。 What would you expect a Scala library to be?
你期望Scala库是什么? Well, it would be a compressed set of .class files.
好吧,它将是一组压缩的.class文件。 Both languages are compiled to Java byte-code!
两种语言都编译为Java字节码! So, to answer your questions:
那么,回答你的问题:
Does anyone else think fewer 3rd party libraries (compared to Java) is a barrier to using Scala in commercial projects?
是否有人认为较少的第三方库(与Java相比)是在商业项目中使用Scala的障碍?
No, not really. 不,不是真的。
if you can call existing Java libraries does it even matter, or does having to span two different languages in a codebase make it harder?
如果你可以调用现有的Java库甚至是重要的,或者在代码库中不得不跨越两种不同的语言会让它更难吗?
If you're looking at the java-library as a compiled .jar file, you wouldn't have to span two different languages. 如果您将java库看作已编译的.jar文件,则不必跨越两种不同的语言。
Edit: Sure, Scala has a very rich type system which compiled class-files can't make full use of. 编辑:当然,Scala有一个非常丰富的类型系统,编译类文件无法充分利用。 On the contrary, the vast number of useful librarys available in Java byte-code format probably makes Scala more attractive than other new (read modern) languages.
相反,Java字节码格式的大量有用库可能使Scala比其他新的(现代阅读)语言更具吸引力。
I recommend this article (and pattern): 我推荐这篇文章(和模式):
Whenever a Java library has an interface that is just a little bit cumbersome in Scala, this is a great way to make it more convenient and your code more elegant. 每当Java库的接口在Scala中有点麻烦时,这是一种使它更方便,代码更优雅的好方法。 Here's a very simple example.
这是一个非常简单的例子。 I want to use the Scala XML pretty printer all the time in my code to return nicely formatted XML.
我想在我的代码中一直使用Scala XML pretty打印机来返回格式良好的XML。 This is the normal way to do that:
这是正常的方法:
class Service {
val pp = new scala.xml.PrettyPrinter(80,2)
def content =
pp.format(<foo><bar>{something()}</bar></foo>)
}
however since I do this all the time I add this to my package 但是,因为我一直这样做,所以我将它添加到我的包中
import scala.xml.Elem
object PrettyXml {
val pp = new scala.xml.PrettyPrinter(80,2)
}
trait PrettyXml {
case class Formatted(xml:Elem) {
def pretty = PrettyXml.pp.format(xml)
}
implicit def toFormatted(xml:Elem) = Formatted(xml)
}
and now I can replace my original code with 现在我可以用我的原始代码替换
class Service extends PrettyXML {
val pp = new scala.xml.PrettyPrinter(80,2)
def content =
<foo><bar>{something()}</bar></foo> pretty
}
If I didn't want to make it a trait I could probably put PrettyXML in a package object. 如果我不想让它成为一个特性,我可能会把PrettyXML放在一个包对象中。
This is actually not as stupid and misguided a question as it seems. 这实际上并不像看起来那样愚蠢和误导一个问题。 Some truths about libraries in Scala:
关于Scala库的一些事实:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.