What is a best practice for OCaml variants with many fields?

Max Heiber

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.

Maëlan

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best practice to protect some fields in a entity for being edited?

From Dev

What is the best way to render many input fields in React?

From Java

What is the best practice for RestController?

From Dev

What is AJAX best practice?

From Dev

OCaml order of pattern matches best practice

From Dev

What is best practice for modeling a SaaS app with Firebase on a many-to-many relationship

From Dev

transform SQL query to rails query with many to many relations, what is the best practice?

From Dev

SQL one to many, best practice?

From Dev

best-practice for ordered many to many relation?

From Dev

What is best practice for dealing with blank ('') string values on non-nullable model fields?

From Dev

What is best practice to serialize foreign key fields in a REST-ful api

From Dev

What's the best practice to fetch specific fields from big data coming from RESTful service in Angular?

From Dev

What's the best practice to design a table that would have different fields on different conditions?

From Dev

In 1-to-many Rails ActiveRecord relation, what is the best practice to get parent records that they only have children records?

From Dev

what is best practice to control "too many local variable in a function" without suppress and manipulate pylint settings?

From Dev

What is the best practice of using CompositeDisposables

From Dev

What is the best practice for logging in Umbraco?

From Dev

What is the best practice to get Looper?

From Dev

what is best practice? An attribute or an Entity?

From Dev

What is the best practice in RESTful URL?

From Dev

What is the best practice to normalize CSS?

From Dev

what is the best practice to import library?

From Dev

What is the best practice to improve performance?

From Dev

What is the best practice for password encryption?

From Dev

variants in modern c++, what is the best way to do them

From Java

Best practice to choose fields for equals() implementation

From Dev

Kotlin best practice: val of object fields

From Dev

Is it best practice to create signature fields with relation values?

From Dev

Best practice for overriding class fields in Dart

Related Related

  1. 1

    What is the best practice to protect some fields in a entity for being edited?

  2. 2

    What is the best way to render many input fields in React?

  3. 3

    What is the best practice for RestController?

  4. 4

    What is AJAX best practice?

  5. 5

    OCaml order of pattern matches best practice

  6. 6

    What is best practice for modeling a SaaS app with Firebase on a many-to-many relationship

  7. 7

    transform SQL query to rails query with many to many relations, what is the best practice?

  8. 8

    SQL one to many, best practice?

  9. 9

    best-practice for ordered many to many relation?

  10. 10

    What is best practice for dealing with blank ('') string values on non-nullable model fields?

  11. 11

    What is best practice to serialize foreign key fields in a REST-ful api

  12. 12

    What's the best practice to fetch specific fields from big data coming from RESTful service in Angular?

  13. 13

    What's the best practice to design a table that would have different fields on different conditions?

  14. 14

    In 1-to-many Rails ActiveRecord relation, what is the best practice to get parent records that they only have children records?

  15. 15

    what is best practice to control "too many local variable in a function" without suppress and manipulate pylint settings?

  16. 16

    What is the best practice of using CompositeDisposables

  17. 17

    What is the best practice for logging in Umbraco?

  18. 18

    What is the best practice to get Looper?

  19. 19

    what is best practice? An attribute or an Entity?

  20. 20

    What is the best practice in RESTful URL?

  21. 21

    What is the best practice to normalize CSS?

  22. 22

    what is the best practice to import library?

  23. 23

    What is the best practice to improve performance?

  24. 24

    What is the best practice for password encryption?

  25. 25

    variants in modern c++, what is the best way to do them

  26. 26

    Best practice to choose fields for equals() implementation

  27. 27

    Kotlin best practice: val of object fields

  28. 28

    Is it best practice to create signature fields with relation values?

  29. 29

    Best practice for overriding class fields in Dart

HotTag

Archive