News for hackers. Stuff that matters.
Re:The type system is a programmer's best friend by mikewarot on Saturday November 05, 2022 @09:02AM (#33481006)
I once attended a meeting where a Professor from a University somewhere in Chicago gave a brilliant demonstration of using a similar type system for dealing with values in Electrical Engineering. It made quite sure you couldn't do things like add volts and amps.

It was in C++ if I recall correctly.

This is a great idea, that I've haven't had cause to use yet.

Re:The type system is a programmer's best friend by mdm12 on Saturday November 05, 2022 @09:32AM (#33481294)
Unit of measures are a great example of what a type system can do, and something not enough languages support. F#[1] and Scala[2] are two that I know of that do support UOMs. Like you, I haven't had the need to use them in the domains I work in, but I imagine that they would be invaluable in certain contexts.

[1] https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...

[2] https://github.com/typelevel/squants

Re:The type system is a programmer's best friend by leephillips on Saturday November 05, 2022 @10:17AM (#33481693)
Julia has this via a package: Measurements.jl.
Re:The type system is a programmer's best friend by docandrew on Saturday November 05, 2022 @10:23AM (#33481741)
Re:The type system is a programmer's best friend by throwawaymaths on Saturday November 05, 2022 @10:33AM (#33481844)
It's also something that some languages seriously screw up. Consider multiplying a time (which is typed in go) with a numerical value... suppose what I want is a user to input number of time intervals to wait. So the user wants 5, and the interval is 2500 milliseconds. The way you get 12500 milliseconds out of that made me want to throw my computer out the window.
Re:The type system is a programmer's best friend by Chinjut on Saturday November 05, 2022 @12:42PM (#33483096)
I don't understand. What should you get instead?
Re:The type system is a programmer's best friend by morelisp on Saturday November 05, 2022 @04:30PM (#33485331)
Go does not have operator overloading, and numeric operators must have identical types. So if you have `var x int = 5` and `var t time.Duration = 2500 * time.Millisecond`, you have to `time.Duration(5) * t` or `time.Duration(x * int(t))`.

It's slightly better than languages with no operator overloading nor newtypes at all (well, actually a lot better given other things you can use newtypes for) but without operator overloading using it just for units, with no other API machinery, is usually a bad idea.

Re:The type system is a programmer's best friend by throwawaymaths on Monday November 07, 2022 @11:28AM (#33507982)
Two options:

1. Don't type units like that.

2. Allow the * operator to multiply a time-unitful value with an untimed scalar and disallow using it with two time-unitful values.

Presumably the requirement that * operands are the same is arbitrarily modifiable and go developers have control over what types operators take.

Re:The type system is a programmer's best friend by morelisp on Monday November 07, 2022 @01:44PM (#33510244)
1. Uh, ok. Might as well throw out the whole thread then?

2. A "time unit" is not a special type of value. You can construct arbitrary types of integers, and it is common to do so. `*` has no clue what a time is, just that it's "not an int" (for example).

Re:The type system is a programmer's best friend by xigoi on Saturday November 05, 2022 @05:16PM (#33485869)
The commenter you're replying to expressed it confusingly. The point is that in Go, 5 * time.Milliseconds(2500) is a type error, and instead you need to do time.Nanoseconds(5) * time.Milliseconds(2500).
Re:The type system is a programmer's best friend by morelisp on Saturday November 05, 2022 @05:39PM (#33486109)
`5 * time.Milliseconds(2500)` is not a type error, though `int(5) * time.Milliseconds(2500)` is.

(This is especially relevant because you really mean `5 * (2500 * time.Millisecond)` vs. int(5) * (2500 * time.Millisecond)`, as there is no `time.Milliseconds` function.)

Re:The type system is a programmer's best friend by xigoi on Saturday November 05, 2022 @05:58PM (#33486299)
Thanks, I don't remember exactly how it worked. It doesn't take away from the stupidity.
Re:The type system is a programmer's best friend by masklinn on Saturday November 05, 2022 @09:27AM (#33481251)
FWIW that's a pretty basic application called "units of measure". Some languages like F# have that natively.
Re:The type system is a programmer's best friend by mrkeen on Saturday November 05, 2022 @09:32AM (#33481291)
I wish static typing were as uncontentious as units of measure.
Re:The type system is a programmer's best friend by masklinn on Saturday November 05, 2022 @09:34AM (#33481310)
I wouldn't say that UOM are uncontentious, things can get dicey around reference units and precision for instance, or the combinatorial explosion of composite units.
Re:The type system is a programmer's best friend by mrkeen on Saturday November 05, 2022 @09:48AM (#33481443)
Right, but if you told your professor you sometimes represent distance in Volts (to make your calculations simpler) you'd get some funny looks.

You could even double-down on it: "Have there been any studies that prove that using units of measure helps you get the right answer?"

Re:The type system is a programmer's best friend by tsimionescu on Saturday November 05, 2022 @01:50PM (#33483790)
Funnily enough, there are some applications for converting mechanics problems to analogous electricity problems to leverage circuit simulation software such as PSPICE to help solve things like transcendental equations.
Re:The type system is a programmer's best friend by hither_shores on Saturday November 05, 2022 @03:47PM (#33484909)
Yes, but the mapping doesn't change the relationship between the units of measure, which is the actual meaning as far as the type system is concerned. It's just a change of names.
Re:The type system is a programmer's best friend by tsimionescu on Saturday November 05, 2022 @03:49PM (#33484929)
Sure, I wasn't meaning to detract from the comment above, just to point out what I thought was an interesting related fact.
Re:The type system is a programmer's best friend by photochemsyn on Saturday November 05, 2022 @09:45AM (#33481423)
This approach to typing could have saved the Mars Climate Orbiter, i.e a pounds of force type vs. a newtons type.

> "A NASA review board found that the problem was in the software controlling the orbiter's thrusters. The software calculated the force the thrusters needed to exert in pounds of force. A separate piece of software took in the data assuming it was in the metric unit: newtons.... Propulsion engineers, like those at Lockheed Martin who built the craft, typically express force in pounds, but it was standard practice to convert to newtons for space missions. One pound of force is about 4.45 newtons. Engineers at NASA's Jet Propulsion Lab assumed the conversion had been made, and didn't check."

https://www.wired.com/2010/11/1110mars-climate-observer-repo...

There could be issues with memory use, but it could also be implemented as an API, i.e. ensuring values exported from one software package to another were of the correct type, but then store them internally as simple types... Switching everything to a unified metric system would make more sense in the long run, however.

Re:The type system is a programmer's best friend by est on Saturday November 05, 2022 @09:48AM (#33481446)
More like an i18n or l10n issue than types.
Re:The type system is a programmer's best friend by morelisp on Saturday November 05, 2022 @04:35PM (#33485388)
I think you've inadvertently stumbled on another great example, distinct types for TranslatedMessage, LocalizedNumber, etc. from ordinary string has been a cornerstone of localization enforcement on at least two large applications I've worked on.
Re:The type system is a programmer's best friend by wirthjason on Saturday November 05, 2022 @10:45AM (#33481955)
I’m not sure the Chicago reference but if you’re talking about C++ the Units library is a good option.

https://github.com/mpusz/units

Re:The type system is a programmer's best friend by hgomersall on Saturday November 05, 2022 @12:47PM (#33483152)
It's possible to go even further than just protecting against the wrong unit in the wrong place. You can generalise units: https://docs.rs/dimensioned/latest/dimensioned/

(Edit, that's not possible in c++)

Re:The type system is a programmer's best friend by jdrek1 on Saturday November 05, 2022 @01:38PM (#33483660)
> (Edit, that's not possible in c++)

Mind expanding on that? Because what that readme there shows is absolutely possible in C++, I have used a similar system for dealing with natural units.

Re:The type system is a programmer's best friend by hgomersall on Saturday November 05, 2022 @02:00PM (#33483926)
I'm happy to admit I'm wrong on that. My understanding is the bit that makes (kg.m)/s^2 type equivalent to a N, equivalent to J/m is not implementable in the same generic way.
Re:The type system is a programmer's best friend by pencilguin on Saturday November 05, 2022 @04:42PM (#33485460)
All unit systems decompose each unit to primitives, that can then be aliased for notational convenience. So, a result of J/m decomposes the same as a N.
Re:The type system is a programmer's best friend by tsimionescu on Saturday November 05, 2022 @01:46PM (#33483745)
Note that most such solutions break (or become much much much more complex) if you want anything more than simple arithmetic from them. For example, matrix multiplication with typed values (where each element of the matrix can have a different type/unit of measure) is extremely ugly code, and basically no such library supports it - even for matrices of fixed size (say, code that could multiply 3x3 matrices with 9 type parameters - which is not unrealistic in physical simulations).

This is a fundamental limitation of types - they tend to scale poorly to very complex non-uniform structures. That's not to say that they shouldn't be used when they do scale nicely, though!

Re:The type system is a programmer's best friend by tremon on Saturday November 05, 2022 @03:25PM (#33484731)
matrix multiplication with typed values (where each element of the matrix can have a different type/unit of measure)

Is this really a common occurrence? In most situations I've come across, it's the matrix itself (rows/columns) that has a unit of measurement, not the invididual columns. Tensors, rotation matrices, lighting maps: they all use the same units of measurement.

Re:The type system is a programmer's best friend by tsimionescu on Saturday November 05, 2022 @03:43PM (#33484875)
Matrix multiplication is often used for solving systems of linear equations, and you often have systems of linear equations involving different physical quantities (such as position, speed, time and mass if solving some classical mechanics equations, or pressure, volume, temperature, and time for thermodynamics etc).

And while it may be relatively common to start out and end up with matrices that have a single unit for each row (but different units on different rows), intermediate results will often end up with different combinations of units in each element.