N-ary tuples vs pairs

后端 未结 1 777
夕颜
夕颜 2021-01-18 00:35

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, (         


        
相关标签:
1条回答
  • 2021-01-18 01:12

    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.

    0 讨论(0)
提交回复
热议问题