Open cloudRoutine opened 8 years ago
Would there be a single ad-hoc Discriminated Union for all polymorphic variants currently in scope? Then depending on program structure and scope different sets of polymorphic variants would be available. I think I like this idea...
Add Polymorphic Variants (ad-hoc Discriminated Unions) [12425811]
Submitted by Jared Hester on 2/21/2016 12:00:00 AM
[ 36 votes ]
Creating strongly typed data constructs with some level of heterogeneity is often accomplished with DUs so lets take the trivial case of wanting to have lists of floats & ints, ints & strings, and ints & floats & strings We'd need the types - type IntString = | Int of int | String' of string type FloatInt = | Float of int | Int of int
type IntFloatString = | Float of int | Int of int | String' of string But in practice using these would require qualified access to make sure you're getting the right case from the right type and as types you want to group grow in number there's a combinatory explosion of boilerplate and verbose code necessary to put it to work. Polymorphic variants cut down the noise and increase the flexibility and expressiveness of your code. The declaration of a polymorphic variant in OCaml is not unlike that of the single case DUs that are already commonly found in F# code Instead of - type Int = Int of int;;