简体   繁体   English

为什么从Java和C#中删除const?

[英]Why was constness removed from Java and C#?

I know this has been discussed many times, but I am not sure I really understand why Java and C# designers chose to omit this feature from these languages. 我知道这已经讨论了很多次,但我不确定我是否真的理解为什么 Java和C#设计者选择从这些语言中省略这个功能。 I am not interested in how I can make workarounds (using interfaces, cloning, or any other alternative), but rather in the rationale behind the decision. 我对如何制定变通方法(使用接口,克隆或任何其他替代方法)不感兴趣,而是对决策背后的基本原理感兴趣。

From a language design perspective, why has this feature been declined? 从语言设计的角度来看,为什么这个功能被拒绝了?

PS: I'm using words such as "omitted", which some people may find inadequate, as C# was designed in an additive (rather than subtractive) approach. PS:我正在使用诸如“省略”之类的词语,有些人可能觉得这些词语不合适,因为C#是用加法(而不是减法)方法设计的。 However, I am using such words because the feature existed in C++ before these languages were designed, so it is omitted in the sense of being removed from a programmer's toolbox. 但是,我使用这样的词是因为在设计这些语言之前C ++中存在这个特性,所以从程序员的工具箱中删除它就省略了。

In this interview, Anders said: 这次采访中,安德斯说:

Anders Hejlsberg: Yes. Anders Hejlsberg:是的。 With respect to const, it's interesting, because we hear that complaint all the time too: "Why don't you have const?" 关于const,它很有意思,因为我们也一直听到这样的抱怨:“为什么你没有const?” Implicit in the question is, "Why don't you have const that is enforced by the runtime?" 问题隐含的是,“为什么你没有运行时强制执行的const?” That's really what people are asking, although they don't come out and say it that way. 这正是人们所要求的,尽管他们并没有这样说出来。

The reason that const works in C++ is because you can cast it away. const在C ++中工作的原因是因为你可以把它丢弃。 If you couldn't cast it away, then your world would suck. 如果你不能把它扔掉,那么你的世界就会糟透了。 If you declare a method that takes a const Bla, you could pass it a non-const Bla. 如果声明一个采用const Bla的方法,则可以将它传递给非const Bla。 But if it's the other way around you can't. 但如果它是相反的方式你不能。 If you declare a method that takes a non-const Bla, you can't pass it a const Bla. 如果声明一个采用非const Bla的方法,则不能将它传递给const Bla。 So now you're stuck. 所以现在你被卡住了。 So you gradually need a const version of everything that isn't const, and you end up with a shadow world. 所以你逐渐需要一个不是const的所有东西的const版本,你最终得到了一个阴影世界。 In C++ you get away with it, because as with anything in C++ it is purely optional whether you want this check or not. 在C ++中,你可以使用它,因为与C ++中的任何东西一样,无论你是否想要这个检查,它都是纯粹可选的。 You can just whack the constness away if you don't like it. 如果你不喜欢它,你可以打破常量。

I guess primarily because: 我猜主要是因为:

  • it can't properly be enforced, even in C++ (you can cast it) 它无法正确执行,即使在C ++中(你可以强制转换)
  • a single const at the bottom can force a whole chain of const in the call tree 底部的单个const可以强制调用树中的整个const链

Both can be problematic. 两者都有问题。 But especially the first: if it can't be guaranteed, what use is it? 但特别是第一个:如果不能保证,它有什么用? Better options might be: 更好的选择可能是:

  • immutable types (either full immutability, or popsicle immutability) 不可变类型(完全不变性或冰棒不变性)

As to why they did it those involved have said so: 至于他们为什么这么做,那些参与者都这么说:

http://blogs.msdn.com/ericgu/archive/2004/04/22/118238.aspx http://blogs.msdn.com/slippman/archive/2004/01/22/61712.aspx also mentioned by Raymond Chen http://blogs.msdn.com/oldnewthing/archive/2004/04/27/121049.aspx 雷蒙德也提到http://blogs.msdn.com/ericgu/archive/2004/04/22/118238.aspx http://blogs.msdn.com/slippman/archive/2004/01/22/61712.aspxhttp://blogs.msdn.com/oldnewthing/archive/2004/04/27/121049.aspx

In a multi language system this would have been very complex. 在多语言系统中,这将非常复杂。

As for Java, how would you have such a property behave? 至于Java,你会如何表现这样的属性? There are already techniques for making objects immutable, which is arguably a better way to achieve this with additional benefits. 已经存在使对象不可变的技术,这可以说是通过额外的好处实现这一目标的更好方法。 In fact you can emulate const behaviour by declaring a superclass/superinterface that implements only the methods that don't change state, and then having a subclass/subinterface that implements the mutating methods. 实际上,您可以通过声明仅实现不更改状态的方法的超类/超接口来模拟const行为,然后使用实现变异方法的子类/子接口。 By upcasting your mutable class to an instance of class with no write methods, other bits of code cannot modify the object without specifically casting it back to the mutable version (which is equivalent to casting away const ). 通过将可变类转发到没有写入方法的类的实例,其他代码不能修改对象而不将其专门地转换回可变版本(这相当于抛弃const )。

Even if you don't want the object to be strictly immutable, if you really wanted (which I wouldn't recommend) you could put some kind of 'lock' mode on it so that it could only be mutated when unlocked. 即使你不希望对象是严格不可变的,如果你真的想要(我不推荐),你可以在它上面设置某种“锁定”模式,这样它只能在解锁时进行变异。 Have the lock/unlock methods be private, or protected as appropriate, and you get some level of access control there. 将锁定/解锁方法设置为私有或适当保护,并在那里获得某种级别的访问控制。 Alternatively, if you don't intend for the method taking it as a parameter to modify it at all, pass in a copy of that object, or if copying the entire object is too heavyweight then some other lightweight data object that contains just the necessary information. 或者,如果您不打算将该方法作为参数进行修改,则传入该对象的副本,或者如果复制整个对象太重,那么其他一些轻量级数据对象只包含必要的信息。 You could even use dynamic proxies to create a proxy to your object that turn any calls to mutation methods into no-ops. 您甚至可以使用动态代理为对象创建代理,将任何对变异方法的调用转换为无操作。

Basically there are already a whole bunch of ways to prevent a class being mutated, which let you choose one that fits most appropriately into your situation (hint: choose pure immutability wherever possible as it makes the object trivially threadsafe and easier to reason with in general). 基本上已经有很多方法可以防止类被突变,这让你可以选择一个最适合你情况的方法(提示:尽可能选择纯粹的不变性,因为它使对象简单地线程安全并且通常更容易推理)。 There are no obvious semantics for how const could be implemented that would be an improvement on these techniques, it would be another thing to learn that would either lack flexibility, or be so flexible as to be useless. 没有明显的语义可以实现const是如何实现这些技术的改进,这将是另一件事,要么学习要么缺乏灵活性,要么灵活到无用。

That is, unless I've missed something, which is entirely possible. 也就是说,除非我错过了什么,这是完全可能的。 :-) :-)

Java have its own version of const; Java有自己的const版本; final. 最后。 Joshua Bloch describes in his Effective Java how you effectively use the final keyword. Joshua Bloch在他的Effective Java中描述了如何有效地使用final关键字。 (btw, const is a reserved keyword in Java, for future discrepancies) (顺便说一句,const是Java中的保留关键字,用于未来的差异)

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

相关问题 为什么C#不提供类似于C ++的constness? - Why doesn't C# offer constness akin to C++? 从Java到c#PBKDF2WithHmacSHA1填充无效,无法删除 - from java to c# PBKDF2WithHmacSHA1 Padding is invalid and cannot be removed 为什么4月份从All-In-One代码框架中删除了所有C#Shell扩展示例? - Why were all the C# Shell Extension Examples removed from the All-In-One code framework in April? 填充无效且无法删除 - 在 c# 中解密(在 java 中完成加密) - Padding is invalid and cannot be removed - decryption in c# (encryption done in java) 从组合框删除所有项目后,C#显示MessageBox - C# Display MessageBox when all items from ComboBox are removed 从哈希表中删除元素时,c#如何缩小? - c# how hashtable shrinks when elements are removed from hashtable? 从C#中的TabControl中删除tabPage时引发什么事件 - what event is raised when a tabPage is removed from a TabControl in C# c#生成的代码引用从项目中删除的程序集 - c# generated code refers to assembly removed from project 如何从c#中的按位右移中删除位 - How to get the bit removed from the bitwise right shift in c# 从列表中删除C#对象会丢失引用 - c# object loses reference when removed from list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM