Java 有了闭包之后 Scala 的优势

发布于 2024-09-25 18:56:29 字数 1435 浏览 4 评论 0原文

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

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

发布评论

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

评论(3

狂之美人 2024-10-02 18:56:29

除了闭包(Java 似乎还没有拥有闭包)之外,这里列出了 Scala 中 Java 所缺少的功能。我将在这里省略库并专注于语言本身的功能。无论如何,这并不全面,但我认为它包含了大件物品。

  • 隐式参数/转换
  • 模式匹配、案例类
  • 类型推断(一些)
  • 更高-kinded types(类型构造函数的抽象)
  • Monadic for 推导
  • 式 方差
  • 注释具有行为(特征)的接口
  • 默认参数和命名参数
  • 统一方法和运算符(方法可以用作中缀运算符,运算符可以被重载,因为它们只是方法)
  • 统一类型层次结构;没有基本类型
  • 属性而不是 getter 和 setter
  • 抽象值
  • 一流的不可变引用(valvar 一样容易声明)
  • 按名称(惰性)术语 (也许Java闭包会让这变得相当容易表达)
  • 一些尾部调用递归优化
  • 抽象类型类型
  • 别名
  • 自类型路径
  • 依赖类型
  • 结构类型
  • 类型ascription,与类型转换不同重
  • 命名导入
  • 一流模块(对象)
  • 一流的包
  • 具体化泛型(清单 )
  • 分隔延续

这些构建块启用了一些很酷的辅助构造:

  • 类型类(通过隐式参数和更高类型)
  • Pimp My Library” 模式(通过隐式转换)
  • 内部 DSL(通过运算符重载和中缀方法)
  • 解析器组合器(由高阶函数启用并通过中缀方法变得漂亮)
  • 生成器、协程、自定义控制结构(通过分隔延续)
  • 类型级编程(通过更高阶函数 ) -kinded 和抽象类型)
  • 依赖注入框架的过时(通过 蛋糕模式< /a>)

最后,我要提到 Scala 有一个 REPL(读取-评估-打印-循环)——实际上并不是语言本身的功能,但拥有它真是太好了!

Apart from closures (which Java doesn't appear all that close to having), here's a list of features in Scala that are missing from Java. I'll omit libraries here and concentrate on the features of the language itself. This is not comprehensive by any means, but I think it contains the big ticket items.

  • Implicit parameters / conversions
  • Pattern matching, case classes
  • Type inferencing (some)
  • Higher-kinded types (abstraction over type constructors)
  • Monadic for comprehensions
  • Variance annotations
  • Interfaces with behavior (traits)
  • Default and named arguments
  • Unified methods and operators (methods can be used as infix operators, operators can be overloaded because they're just methods)
  • Unified type hierarchy; no primitive types
  • Properties rather than getters and setters
  • Abstract values
  • First-class immutable references (vals are as easy to declare as vars)
  • By-name (lazy) terms (maybe Java closures would make this reasonably easy to express)
  • Some tail-call recursion optimization
  • Abstract types
  • Type aliasing
  • Self types
  • Path-dependent types
  • Structural types
  • Type ascription, as distinguished from type casting
  • Renaming imports
  • First-class modules (objects)
  • First-class packages
  • Reified generics (manifests)
  • Delimited continuations

Some cool secondary constructs that these building blocks enable:

  • Type classes (via implicit parameters and higher-kinded types)
  • The "Pimp My Library" pattern (via implicit conversions)
  • Internal DSLs (via operator overloading and infix methods)
  • Parser combinators (enabled by higher-order functions and made pretty by infix methods)
  • Generators, coroutines, custom control structures (via delimited continuations)
  • Type-level programming (via higher-kinded and abstract types)
  • Obsolescence of dependency injection frameworks (via the Cake Pattern)

Lastly, I'll mention that Scala has a REPL (read-evaluate-print-loop)--not really a feature of the language itself, but it's very nice to have!

雾里花 2024-10-02 18:56:29

即使 Java 7 [8,..] 有一天会拥有一流的函数,它仍然缺乏隐式、类型推断、强大的 Collections 库、模式匹配、特征和许多其他提高生产力的东西。不谈各种Actor库,丰富的构建DSL的能力,......

Even if Java 7 [8,..] will have first-class functions one day, it still lacks for implicits, type inference, powerful Collections library, pattern matching, traits and lots of other things boosting productivity. Not talking about various Actor libraries, rich capabilities of building DSLs,...

抠脚大汉 2024-10-02 18:56:29

我听说的最新消息是,Java 7 中不会出现闭包。

http://www.baptiste-wicht.com/2010/09/jdk-7-features-updated-plan-b-is-apparently-here/

http://openjdk.java.net/projects/jdk7/features/

第二个链接清楚地将 Lambda 项目(闭包)列出为推迟到 JDK 8。

The latest I've heard is that closures won't make it into Java 7.

http://www.baptiste-wicht.com/2010/09/jdk-7-features-updated-plan-b-is-apparently-here/

http://openjdk.java.net/projects/jdk7/features/

The second link clearly lists project Lambda (closures) as deferred to JDK 8.

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