什么是Java / C#的类型系统的不足? [英] What are the deficiencies of the Java/C# type system?

查看:95
本文介绍了什么是Java / C#的类型系统的不足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其经常听到哈斯克尔(我不知道)有一个很有趣的类型系统..我很熟悉Java和C#的一点点,有时碰巧我打的类型系统,一些设计的容纳的或以某种方式效果更好。

Its often hear that Haskell(which I don't know) has a very interesting type system.. I'm very familiar with Java and a little with C#, and sometimes it happens that I'm fighting the type system so some design accommodates or works better in a certain way.

这使我怀疑...

什么是不知何故发生因为Java的不足的问题/ C#的类型系统?
你如何处理?

What are the problems that occur somehow because of deficiencies of Java/C# type system? How do you deal with them?

推荐答案

阵列被打破。

  Object[] foo = new String[1];
  foo[0] = new Integer(4);



给你java.lang.ArrayStoreException

Gives you java.lang.ArrayStoreException

你对付他们慎用。

为空性是另一个大问题。 NullPointerException异常你的脸跳无处不在。你真的不能做任何事情他们除了切换语言,或避免他们的使用惯例尽可能地(正确初始化场等)。

Nullability is another big issue. NullPointerExceptions jump at your face everywhere. You really can't do anything about them except switch language, or use conventions of avoiding them as much as possible (initialize fields properly, etc).

更一般地, Java的/ C#的类型系统是不是很传神。哈斯克尔可以给你最重要的是,与它的类型,您可以强制函数没有副作用。有一个编译时间证明的程序部分只是被评估表达式使程序更可靠,可组合,更容易推理。 (忽略了一个事实,即哈斯克尔的实现给你的方法来绕过)。

More generally, the Java's/C#'s type systems are not very expressive. The most important thing Haskell can give you is that with its types you can enforce that functions don't have side effects. Having a compile time proof that parts of programs are just expressions that are evaluated makes programs much more reliable, composable, and easier to reason about. (Ignore the fact, that implementations of Haskell give you ways to bypass that).

相比之下,Java的,在调用一个方法几乎可以做任何事情!

Compare that to Java, where calling a method can do almost anything!

此外哈斯克尔有模式匹配,让您创建程序的方式不同而已。您对哪些功能进行操作,往往递归数据。在模式匹配你自毁数据,看它是什么样的,并根据其表现。例如你有一个清单,这要么是空的,或者头部和尾部。如果要计算长度,可以定义说,一个功能:如果列表为空,长度= 0,否则长度= 1 +长度(尾)

Also Haskell has pattern matching, which gives you different way of creating programs; you have data on which functions operate, often recursively. In pattern matching you destruct data to see of what kind it is, and behave according to it. e.g. You have a list, which is either empty, or head and tail. If you want to calculate the length, you define a function that says: if list is empty, length = 0, otherwise length = 1 + length(tail).

如果你真的想了解更多,有两个优秀的在线来源:

If you really like to learn more, there's two excellent online sources:

了解你哈斯克尔真实世界哈斯克尔

这篇关于什么是Java / C#的类型系统的不足?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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