Skip to content

Add F# style units of measure #152

@pmetzger

Description

@pmetzger

This is an idea that has batted about here a bit; I'm writing this down to memorialize some thinking on its own. I don't expect this could actually be done in time for Calc 3.0 (but who knows.)

As we all know, it's all to easy to make mistakes while programming or doing calculations. One way we avoid this during programming is with type systems which catch certain sorts of common errors. Unfortunately, ordinary type systems don't always capture as many of the mistakes we might make as we might want. For example, normal type systems don't always capture dimensions or units of measure, which has had devastating consequences in the real world sometimes.

As it happens the F# language introduced units of measure into its type system some time ago. This means that it is literally impossible to accidentally add inches to seconds in F# if you've used the type system right. It might be interesting to incorporate parts of the F# units system into Calc.

We could imagine a hypothetical future version of Calc in which we can tag values with units. I suspect the syntax below won't work for parsing reasons — the use of <> etc. would probably drive the parser crazy — but let's go with it for the moment for imaginative purposes and we could contemplate syntax (who knows, maybe braces?) that didn't break everything later. For purposes of thinking we might say:

; c=3e8<m/s>
; t=500<s>
; c*t
         150000000000<m>

We might allow ourselves the ability to tag the type we expect a calculation to have so that we'll get an error if it's the wrong type, define derived types (informing the system that <J> = <kg*m^2/s^2>) and even tagging the types of a function's parameters and returns with units so that if (say) we pass 5<1/mol> to something expecting <mol> it gets upset.

A small library could contain the normal Si units and their derived units, as well as conversion functions and the like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions