5 ms·
I did just this very thing in C# a few months ago[1]. The problem I had was that it gets out of hand very quickly. I wanted to be able to divide distance by tim
by moron4hire 13y ago
I did just this very thing in C# a few months ago[1]. The problem I had was that it gets out of hand very quickly. I wanted to be able to divide distance by time and get speed out of it, but it is extremely difficult to satisfy all of the M-to-N relationships between types and still end up being usable.
I ended up abandoning it for the project I'm building, as I wasn't too clear on what the type of a secant of an angle in degrees should be and nobody on the interwebs seemed to care enough to take notice or answer my questions.
[1] https://github.com/capnmidnight/UnitsOfMeasure https://github.com/capnmidnight/UnitsOfMeasure
- jimmaswell 13y agoThe type of a trig function is just a scalar, no unit.
- platz 13y agoF# has this, I wonder how they did it?
- magic_haze 13y agoThey couldn't do it. The compiler does static verifications that, say, a value of type Meter when divided by a value of type second results in a value of type Meter/Second, where '/' is a custom concept built into the F# compiler. The .NET runtime knows nothing about these custom types.
- platz 13y agoAh, I see. Looking at how something similar was implemented in Haskell, they used phantom types and functional dependencies (in the type system) to achieve this - I'm not sure F# has those capabilities.
- moron4hire 13y agoF# has that capability, but .NET does not, so once the F# compiler has done its checking, it drops the info on the floor and you have nothing at runtime. Now, this might not necessarily be a bad thing for F# (I don't know, I'm not an F# user). The runtime type information in .NET is great, but it's all for reflection to be able to build things that a stronger type system like F# has, or a macro system, would be able to handle.
- MichaelGG 13y agoIs that a real issue? They could generate struct types, but why take the performance loss? It only affects people trying to reflect over the unit of measurement types. Additionally, phantom types in F# can make wrapping other types (like int -> AccountId) rather succinct.
- moron4hire 13y agoYeah, that's pretty much exactly the issue I ran into. The problem is that the standard .NET type semantics can only infer "up" the inheritance hierarchy. I had gotten really close. I had types like Base Length: Base Meters: Length Feet: Length Compound<Base, Base> Area<T>: Compound<T, T> where T: Length SquareMeters: Area<Meters> SquareFeet: Area<Feet> and had defined all of my math as generic extension methods of the Base class so that the types would compose: Compound<T, U> Multiply<T, U>(T a, U b) where T : Base where U : Base T Divide<T, U>(Compound<T, U>, U b) where T : Base where U : Base That went a long way towards getting fairly concrete types out of only a few lines of code. But I couldn't get it the rest of the way. C# explicitly forbids user-defined typecasts between types in an inheritance chain with each other, so while multiplying two Meters got me a Compound<Meters, Meters>, it was not then possible to take a Compound<Meter, Meter> and convert it to anything like Area<Meters> or SquareMeters automatically.
- platz 13y agoInteresting problem! I was wondering why even define things like SquareMeters, but as you say the issues is reducing the terms that have different forms but mean the same thing.
- moron4hire 13y agoHaving a SquareMeters type was meant to reduce complexity. Instead of always dealing with Area<Meters, Meters> all the time, it was meant to be a shortcut. Hrm, what about namespace aliasing? "using SquareMeters = Composite<Meters, Meters>"? Since the problem is not the composition of the type but the verbosity of the deeply composited types, then perhaps it's just about making a shortcut for the name.
- platz 13y agothings can be composed different ways. m/s^2 should equal (m/s)/s
- 13y ago