F#: Parametrized Types

Learned something new today. It turns out in current incarnation of F# you can have “parametrized types”. Although it sounds like another flavor of templates, it is nothing like it. These are the types with “units of measure” attached. You can have weight value of type float divided by volume of type float to achieve density of type <kg/m^3>. E.g. 2.5<kg>/5.0<m^3> yields 0.5<kg/m^3>.

The measures exist only for F# compiler. They are “erased” when creating the IL code, so they can’t be queried at runtime. I am not sure yet how generic this can be made and what other applications besides physics it might have.

Posted in

Leave a Reply

Your email address will not be published. Required fields are marked *