简体   繁体   English

是否缺少第三方库阻止您使用Scala?

[英]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): 我推荐这篇文章(和模式):

Pimp my Library 皮条客我的图书馆

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库的一些事实:

  1. Without the vast infrastructure of Java libraries, there would be very little practical reason for most people to use Scala. 如果没有庞大的Java库基础架构,大多数人使用Scala的实际原因就很少了。 Scala is a lovely language with some amazing features, but if you had to reinvent the wheel every time you needed to accomplish anything large, no one would bother. Scala是一种可爱的语言,具有一些惊人的功能,但如果你每次需要完成任何大的工作时都必须重新发明轮子,那么没人会打扰。
  2. Accessing native Java libraries from Scala is slightly more enjoyable than accessing Java libraries from Java, but only slightly. 从Scala访问本机Java库比从Java访问Java库稍微有点愉快,但只是轻微一点。 The problem here is that native Java libraries are considerably lower-level than either libraries written natively in Scala or good Scala wrappers of Java libraries. 这里的问题是本机Java库比Scala本地编写的库或Java库的好Scala包装器低得多。 Native Java libraries tend to be littered with single-method interfaces (that should have been functions), lightly documented nullable arguments or returns (that should have been Option), unnecessary parallel class constructions or "helpers" (that should have been traits), hand-rolled not-quite-standard iterators, try-block-demanding resource management (due to lack of closures) and type signatures (particular with pre Java-1.5 libraries) that are laughably crude. 本机Java库往往散落着单方法接口(应该是函数),轻微记录的可空参数或返回(应该是Option),不必要的并行类构造或“助手”(应该是特征),手工卷制的不太标准的迭代器,需要尝试块的资源管理(由于缺少闭包)和类型签名(特别是Java-1.5库之前的版本),这些都是可笑的粗糙。 It's unsurprising that Scala programmers see Java libraries as "necessary to get the job done" rather than something you actually look forward to manipulating. 毫不奇怪,Scala程序员认为Java库是“完成工作所必需的”,而不是你真正期待操作的东西。
  3. Via either wrapping or pimping, it's actually pretty easy to take a Java native library and make it almost as joyful to use as a Scala native library. 通过包装或拉皮条,实际上很容易使用Java本机库,并使其与Scala本机库一样快乐。 I expect simply gorgeous Scala adapters for most of the major enterprise libraries to become available in the near term. 我希望大多数主要企业库的简单华丽的Scala适配器能够在短期内上市。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM