3 ms·
Used this all the time in University :) I think there's an interesting parallel to this usefulness in code which the author just briefly mentioned. Static typi
by pedrovhb 4y ago
Used this all the time in University :)
I think there's an interesting parallel to this usefulness in code which the author just briefly mentioned. Static typing can help bridge thoughts about what makes sense to do ("I have a callable with signature (x: int, s: str) -> str, and this function I need to use is (s: str) -> str; huh, my x here is fixed, does it make sense to make a partially applied function? yes, that'll work") as well as, of course, verifying all assumptions make sense at some level (types matching is a necessary, though not sufficient, condition).
Then there's more power unlocked by giving dimensions/units to your types/instances. For instance, overloading the division operator in a class representing length so that when dividing length by time, you get speed. Or, one could overload addition/subtraction in classes representing a currency so that trying to add ¥100 to $100 raises an exception.
In general I think there's a lot of interesting applications for the information that comes along with numbers that we usually just discard. Things in the real world aren't dimensionless that often, and yet our code almost always treats them as if they were.
- jessekv 4y agoI had a similar experience in university. I used to take advantage of our mathcad licence. If you specified units for the variables, you could identify mistakes in your equations and functions very quickly. For example, if you wanted meters, but you got m^(2/3), it was usually obvious where in the equation something went wrong. Another bonus was I could focus on the equations and then output the final answer in either metric or franken-imperial, as the homework assignment required (I did my undergrad in the US.)
- sidpatil 4y ago> If you specified units for the variables, you could identify mistakes in your equations and functions very quickly. For example, if you wanted meters, but you got m^(2/3), it was usually obvious where in the equation something went wrong. You can take it even one step further and write tests to check the resulting units for you.
- jessekv 4y agoAh, yes, unit-unit tests. But seriously, I am planning some numerical modelling experiments, and am considering Juila partially because Unitful.jl. Can you recommend it?
- sidpatil 4y agoSome languages feature support for this. F# has it built-in and refers to it as units of measure. Julia has it available as a package named Unitful.jl.
- Archelaos 4y ago> Then there's more power unlocked by giving dimensions/units to your types/instances. For instance, overloading the division operator in a class representing length so that when dividing length by time, you get speed. Or, one could overload addition/subtraction in classes representing a currency so that trying to add ¥100 to $100 raises an exception. I sometimes even go one step further and use specific types for dimensionless factors in financial calculations, such as specific types for the rates of different kinds of taxes. This way I get a compiler error, when I am trying to use the wrong type of tax rate in a calculation.
- swagasaurus-rex 4y agoYou could take it a step further and specify the domain and range of your functions. The division operator for example take any real number except 0 – Zero is a special undefined case specifically for division. Although string types seems intractible, you could easily specify things like [a-zA-Z0-9], or character sets/UTF ranges for limiting inputs to certain languages. All list types (strings, arrays) could specify length as a limit. Programmers would naturally want flexibility with domain enforcement, so could just write a (turing complete) function to which returns the domain of the function in question!