Variants with lots of fields start to get unwieldy. The role of each field ends up in comments:
type ty =
| FunTy of (*all*) ty list * (*params*) ty list * (*return type*) ty
...
Is it a best practice to use record-valued variants in cases like this?
type ty =
| FunTy of ft
...
and ft = {
forall : ty list;
p_tys : ty list;
ret_ty : ty
}
This is easier to read, imo, and gets the documentation out of comments. It also makes pattern-matching easier, since only record fields that matter need to be written-out.
On the other hand, there's an indirection required to dereference the record fields. It doesn't matter for my purposes, but I can see this performance difference making the refactor above unidiomatic in practice.
To keep this answers objectivish, references to the OCaml docs or a style guide would help.
Since OCaml 4.03 (released in 2016) you can use inline records, which give you the nice interface of the record syntax — plus, you can have mutable fields — with the same memory representation as with the tuple syntax.
type ty =
| FunTy of {
forall : ty list;
p_tys : ty list;
ret_ty : ty
}
...
You have some facility to manipulate the contained record, but (not differently than the argument “tuple” in the tuple syntax) it is not a first-class value (see the documentation linked above for more details).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments