N-ary tuples vs pairs

喜你入骨 提交于 2019-12-01 15:10:47

问题


In Ocaml, tuples with different arities have different type and value constructors:

# let a = (1, 2, 3);;
val a : int * int * int = (1, 2, 3)
# let b = (1, (2, 3));;
val b : int * (int * int) = (1, (2, 3))

Note that second example (b) is more flexible than first (a) because "tail" of b - (2, 3) - itself is valid value:

# let (_, c) = b;;
val c : int * int = (2, 3)
# let d = snd b;;
val d : int * int = (2, 3)

What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce infinite (or, even worse, finite) amount of new type and value constructors for different arities?


回答1:


What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce infinite (or, even worse, finite) amount of new type and value constructors for different arities?

The ML type system was designed in the pursuit for stronger static type checking in order to catch as many errors at compile time as possible.

Your suggestion would weaken the type system considerably because it would no longer be able to distinguish between (1, 2, 3) and (1, (2, 3)) which is a move in the opposite direction.

In practice, I can tell you that ML making such distinctions has caught real errors in my production code in the past. I value the ML design in this context.



来源:https://stackoverflow.com/questions/14621559/n-ary-tuples-vs-pairs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!