为什么 Scala 在类中没有静态成员? [英] Why doesn't Scala have static members inside a class?

查看:38
本文介绍了为什么 Scala 在类中没有静态成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以间接定义它们 使用伴随对象实现类似的东西,但我想知道为什么作为一种语言设计,静态变量会从类定义中删除.

I know you can define them indirectly achieve something similar with companion objects but I am wondering why as a language design were statics dropped out of class definitions.

推荐答案

我也在 scala 用户 google group 上发布了这个问题,Bill Venners 是Programming in scala"回复的作者之一,他有一些见解.

I also posted this question on scala users google group and Bill Venners one of the authors of "Programming in scala" reply had some insights.

看看这个:https://groups.google.com/d/msg/scala-user/5jZZrJADbsc/6vZJgi42TIMJhttps://groups.google.com/d/msg/scala-user/5jZZrJADbsc/oTrLFtwGjpEJ

这是摘录:

我认为一个目标只是变得更简单,通过让每个值都是一个对象,每一个操作都是一个方法调用.Java 的静态和原语是特殊情况,这使得语言在某些情况下更加复杂"感觉.

I think one goal was simply to be simpler, by having every value be an object, every operation a method call. Java's statics and primitives are special cases, which makes the language more "complicated" in some sense.

但我认为另一个重要的事情是拥有一些可以映射 Java 的东西静态到 Scala(因为 Scala 需要一些映射的构造到 Java 的互操作静态数据),但这得益于 OO继承/多态.单例对象是真实的对象.他们能扩展一个超类或混合特征并像这样被传递,但是它们本质上也是静态的".事实证明这非常方便练习.

But another big one I think is to have something you can map Java's statics to in Scala (because Scala needed some construct that mapped to Java's statics for interop), but that benefits from OO inheritance/polymorphism. Singleton objects are real objects. They can extend a superclass or mix in traits and be passed around as such, yet they are also "static" in nature. That turns out to be very handy in practice.

另请看一下对 Martin Odersky 的采访(向下滚动到 Scala 部分的面向对象创新)http://www.artima.com/scalazine/articles/goals_of_scala.html

Also take a look at this interview with Martin Odersky (scroll down to Object-oriented innovations in Scala section) http://www.artima.com/scalazine/articles/goals_of_scala.html

这是摘录:

首先,我们希望成为一种纯面向对象的语言,其中每个值都是一个对象,每个操作都是一个方法调用,每个变量都是某个对象的成员.所以我们不想要静态,但我们需要一些东西来代替它们,所以我们创建了单例对象的构造.但即使是单例对象仍然是全局结构.所以挑战是尽可能少地使用它们,因为当你拥有一个全局结构时,你就不能再改变它了.你不能实例化它.很难测试.以任何方式修改它都非常困难.

First, we wanted to be a pure object-oriented language, where every value is an object, every operation is a method call, and every variable is a member of some object. So we didn't want statics, but we needed something to replace them, so we created the construct of singleton objects. But even singleton objects are still global structures. So the challenge was to use them as little as possible, because when you have a global structure you can't change it anymore. You can't instantiate it. It's very hard to test. It's very hard to modify it in any way.

总结:

从函数式编程的角度来看,静态成员通常被认为是不好的(参见这个 由 Gilad Bracha 发布 - Java 泛型之父.它主要与由于全局状态引起的副作用有关).但是 scala 必须找到一种与 Java 互操作的方法(因此它必须支持静态)并最小化(尽管不是完全避免)由于静态而创建的全局状态,scala 决定将它们隔离到伴随对象中.

From a functional programming perspective static members are generally considered bad (see this post by Gilad Bracha - the father of java generics. It mainly has to do with side effects because of global state). But scala had to find a way to be interoperable with Java (so it had to support statics) and to minimize (although not totally avoid) global states that is created because of statics, scala decided to isolate them into companion objects.

伴随对象还具有可扩展的优点,即.利用继承和混合组合(与模拟互操作的静态功能分开).

Companion objects also have the benefit of being extensible, ie. take advantage of inheritance and mixin composition (separate from emulating static functionality for interop).

这篇关于为什么 Scala 在类中没有静态成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆