News for hackers. Stuff that matters.
Re:Strong typing, a hill I'm willing to die on by flohofwoe on Wednesday October 04, 2023 @11:41AM (#37766799)
So... about the strong part in 'strong static typing':

Many years ago I thought that it's a good idea for a game math library to have separate strong types for 'point' (a location in 3D space) and 'vector' (a direction and magnitude in 3D space), and allow/disallow certain operations (e.g. 'point + vector => point' 'vector + vector => vector', 'point - point => vector', while 'point + point' is illegal).

Sounds absolutely great in theory, but in practice it was a royal PITA to work with, but it took me much too long to realize this (how can it be such a hassle when in theory it's such a good idea!)

I soon went back to a general 4D vector class (where a 'point' is defined by .w = 1.0, and a 'vector' by .w = 0.0), and some debug-mode runtime validation (which catches things like trying to add a point to a point).

Of course strong typing also often makes perfect sense, for instance in a 3D rendering API it should be a compilation error to provide a texture-handle where a buffer-handle is expected, but after this experience with points vs vectors (which should've been a classic showcase for strong typing) I would never again "die on that hill" :)

TL;DR: static typing: yes! strong typing: it depends.

Re:Strong typing, a hill I'm willing to die on by Someone on Wednesday October 04, 2023 @12:02PM (#37767154)
> For things like kilometers versus hours I'm still undecided.

Those help you more if you do a lot of math involving both them, but that’s also when it becomes a nightmare in most programming languages because of an explosion in the number of types.

Let’s say your code computes

  3km × 4hours
If so, you need a “km hour” type.

In many languages, you also have to write code to make that happen, and to make it have the same type as

  4hours × 3km
Even if you don’t ever store values with those types in variables, you also may need types for per km, per hour, km² and hour² for expressing the types of intermediate values (for example, in a physics computation, you may encounter √(3km²/4hour² to compute a velocity in km/hour)

And that’s ignoring that you may

- encounter minutes, meters, yards, etc.

- want to use algorithms that compute exp(3km) or log(4hours). What types do these have? Here, you probably want to forget about string typing values.

Re:Strong typing, a hill I'm willing to die on by flohofwoe on Wednesday October 04, 2023 @12:19PM (#37767397)
Apologies, I edited my post while you wrote your reply and removed the kilometers vs hours thing. But your reply makes perfect sense, and I think this "type explosion" is my main gripe with too extremist strong typing.
Re:Strong typing, a hill I'm willing to die on by sneed_chucker on Wednesday October 04, 2023 @01:13PM (#37768345)
That's not a shortcoming of strong or static typing, that's just you realizing that your initial data model was flawed and that you either needed to unify them into a single type (like you did) or utilize an abstract class/interface.

The good thing about stricter typing systems is that it forces you to handle all the cases when you're doing a refactor like this before it compiles.

When changes to the data model happen in large codebases of dynamic code, it frequently gets shipped in a non complete state and turns into a production runtime error later down the line.