1. 29

  2. 3

    That was a really easy to understand explanation of phantom types. thanks.

    1. 3

      It would be interesting to know WHY the compiler complains about unused type parameters in the first place. PhantomData seems like a strange workaround at first.

      1. 2

        The Haskell dimensional library does a great job of using phantom types to represent all physical units in a consistent and compatible way, which allows different units to be combined additively if they are the same and multiplicatively by keeping track of what the resulting units would be. It does this with seven type level natural numbers representing the power that each physical dimension is raised to, so a watt is represented with the units 'Dim 'Pos2 'Pos1 'Neg3 'Zero 'Zero 'Zero 'Zero or length^2*mass^1*time^-3 (with all the units, in order, being length, mass, time, electric current, thermodynamic temperature, amount of substance and luminous intensity). It also tracks at the type level if units are metric or non-metric. When your type system is expecting you to do actual work using it, and not just use it to annotate things for other humans to read later, you can get the compiler to do a lot of work for you, and tell you when you messed up the maths