Why was constness removed from Java and C#?

后端 未结 5 2129
予麋鹿
予麋鹿 2021-02-20 00:45

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. I am not int

5条回答
  •  囚心锁ツ
    2021-02-20 01:14

    I guess primarily because:

    • it can't properly be enforced, even in C++ (you can cast it)
    • a single const at the bottom can force a whole chain of const in the call tree

    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)

提交回复
热议问题