我可以安全地依赖 OCaml 的内置顺序进行枚举吗?
Can I rely safely on OCaml's built-in order for enumerations
我定义了一个类型:
type day = Monday | Tuesday | Wednesday | Thursday
我知道 OCaml 会自动订购任何类型:
val (>=) : 'a -> 'a -> bool
Structural ordering functions. These functions coincide with the usual orderings over integers, characters, strings, byte sequences and floating-point numbers, and extend them to a total ordering over all types.
因此我可以尝试以下方法:
# Monday < Tuesday ;;
- : bool = true
我能否从我的实验中概括出任何求和类型都会按照其构造函数的定义顺序对其进行排序?它记录在某处吗?如果我在我的应用程序中依赖这种行为,它会被认为是不好的做法吗?
上次查看时,我有点惊讶地发现没有记录求和类型的顺序。
如果你有非空的构造函数,那么顺序不是声明的顺序:
# type abc = A | B of int | C;;
type abc = A | B of int | C
# A < B 0;;
- : bool = true
# C < B 0;;
- : bool = true
您可以从求和类型的内部表示中推断出顺序,但我认为它不能保证稳定。
我定义了一个类型:
type day = Monday | Tuesday | Wednesday | Thursday
我知道 OCaml 会自动订购任何类型:
val (>=) : 'a -> 'a -> bool
Structural ordering functions. These functions coincide with the usual orderings over integers, characters, strings, byte sequences and floating-point numbers, and extend them to a total ordering over all types.
因此我可以尝试以下方法:
# Monday < Tuesday ;;
- : bool = true
我能否从我的实验中概括出任何求和类型都会按照其构造函数的定义顺序对其进行排序?它记录在某处吗?如果我在我的应用程序中依赖这种行为,它会被认为是不好的做法吗?
上次查看时,我有点惊讶地发现没有记录求和类型的顺序。
如果你有非空的构造函数,那么顺序不是声明的顺序:
# type abc = A | B of int | C;;
type abc = A | B of int | C
# A < B 0;;
- : bool = true
# C < B 0;;
- : bool = true
您可以从求和类型的内部表示中推断出顺序,但我认为它不能保证稳定。