I've written a lot of C++, and a fair bit of Python. Dynamic typing is seductive at first, but you eventually realize that you would much prefer errors be caught at compile time, rather than run time.
I want a language where I can:
cInches in1, in2, in3;
cCm cm1;
in1 = in2 + in3; // OK
in1 = cm1; // compilation error: cannot mix units
in1 = in2 * in3; // compilation error: square inches are not inches
in1 = 2.0 * in2; // OK
float f1 = in1 / in2; // OK
cCm foo(cInches in)
{ return cCm(in * 2.54);
}
foo(cm1); // compilation error
Naturally this is accomplished with special classes (not shown here), it is not an inherent property of the language.
Both are measures of length. I can add an inch and a centimeter, and the result is a well defined length that we can express in inches, centimeters, light years, or any other measure of length. Likewise for areas or ratios of lengths.
I would generally prefer it if the language just gave me the correct result, which I can then print out in the user's preferred unit.
Lengths are just as absolute as integers, but we have different ways to input and represent them. A sufficiently advanced language understands the equivalences and does just the right thing. Adding apples and oranges does not happen.
Generally your function should not expect inches, it should expect some length and I can input it in any unit of length I want.
Sure there are cases where you're constrained and need to account for the limited range and precision of your underlying storage format, but that should not be the default for any modern high level general purpose programming language. We don't need to concern ourselves with grade school stuff, the computer can do it for us.
Adding apples to oranges is concern at a lower level than the abstraction we should be working with by default. Doing arithmetic by hand on pen and paper is pretty low level.
Adding apples to oranges is also a concern when you don't have proper types by which the system can deduce that orange is actually just 2.54*apple.
You're missing the point. I make an app that prints charts. It includes a function that subtracts user-specified margins from the paper width, to calculate available space. Some of my customers are in the U.S (inches), others are not (cm).
If I call
printPage(0.75, 0.75);
you really think it's going to be all the same whether "0.75" represents inches or centimetres?
I am not sure if your "I want a language where I can" is followed by an implicit ", and C++ is that language" or ", I wish it existed".
I am guessing the former as your example is possible with C++ but it doesn't seem like a great example as you are using units of measure for your types. There are languages that support those and obviously, in such a case, assigning centimeters or inches to a distance-typed variable is both ok. The type error would be in mixing distance and, say, weight.
I meant C++. I'm using this currently in C++ production code. Has saved me tons of debugging.
What I meant was that if I'm going to look at a new language, I would expect it to support this type of functionality.
I suppose I can enhance the classes and overload the operators to automatically convert from cm to inches when doing mixed arithmetic. But this will quickly become insanely complicated when considering all possible permutations of arithmetic operators.
I want a language where I can:
Naturally this is accomplished with special classes (not shown here), it is not an inherent property of the language.