D中的const和immutable有什么区别? [英] What is the difference between const and immutable in D?

查看:35
本文介绍了D中的const和immutable有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

D中的constimmutable类型限定符有什么区别?

What is the difference between the const and immutable type qualifiers in D?

推荐答案

const 不能通过该引用进行变异,但可以通过对相同数据的可变引用进行变异.immutable 不能通过 any 对该数据的引用来改变.所以,如果你有

Something that is const cannot be mutated via that reference but could be mutated by a mutable reference to the same data. Something that is immutable can't be mutated by any reference to that data. So, if you have

const C c = foo();

那么你就知道不能通过c改变c所引用的对象,但是c所引用的对象的其他引用可能存在于你的代码中,如果它们是可变的,它们可以改变它,从而改变 c 看到的内容.但是如果你有

then you know that you cannot mutate the object referred to by c through c, but other references to the object referred to by c may exist in your code, and if they're mutable, they could mutate it and therefore change what c sees. But if you have

immutable C c = foo();

那么你就知道c 引用的对象不可能改变.一旦构造了 immutable 对象,对其进行变异是非法的,除非您通过强制转换来颠覆类型系统,否则甚至不可能对 immutable对象.并且由于如果编译器选择将 immutable 对象放入只读内存中,那么如果您曾经尝试抛弃 immutable 并进行变异,您实际上可能会遇到段错误等物体.const 也是如此,因为 const 引用实际上可以引用 immutable 对象.抛弃 constimmutable 然后改变当时可变的对象是未定义的行为,基本上不应该这样做.

then you know that it's not possible for the object referred to by c to change. Once an immutable object has been constructed, it's illegal for it to be mutated, and unless you subvert the type system via casting, it's not even possible to have a mutable reference to an immutable object. And since immutable objects can be put into read-only memory if the compiler chooses to, you could actually get segfaults and the like if you ever tried to cast away immutable and mutate the object. The same goes for const, since a const reference could actually refer to an immutable object. Casting away either const or immutable and then mutating the then mutable object is undefined behavior and should basically never be done.

而且由于 immutable 对象永远不会被另一个引用改变,因此从多个线程读取 immutable 对象是完全线程安全的.因此,immutable 对象在线程间隐式共享,而其他所有未显式标记为 shared 的对象都被视为线程本地的.immutable 还为编译器提供了比 const 更好的优化机会,因为它保证永远不会改变,而 const 对象可以通过另一个引用来改变到相同的数据.

And since an immutable object can never be mutated even by another reference, reading an immutable object from multiple threads is completely thread-safe. So, immutable objects are implicitly shared across threads, whereas everything else which isn't explicitly marked with shared is considered thread-local. immutable also provides better optimization opportunities to the compiler than const does, because it's guaranteed to never change, whereas a const object can change through another reference to the same data.

对于值类型,constimmutable 之间并没有太大区别(因为您不能对非可变值类型进行可变引用),但是对于引用类型,存在显着差异.

For value types, there isn't really much difference between const and immutable (since you can't have mutable references to non-mutable value types), but for reference types, there is a significant difference.

这篇关于D中的const和immutable有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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