baq 4 days ago

strong typing: 2 + "2" is an error (e.g. Python vs JS)

static typing: 2 + "2" does not compile/parse (e.g. Python vs mypy, Typescript vs JS)

this is a very simplistic example, but should get you to feel the difference.

2
volemo 3 days ago

> static typing: 2 + "2" does not compile/parse (e.g. Python vs mypy, Typescript vs JS)

I think this example is not correct, because static typing doesn’t affect how values of different types interact. And while I don’t know of any staticly typed language where specifically `2 + “2”` is a valid expression, statically typed languages definitely can be weakly typed: the most prominent example is C where one can combine values of different types without explicitly converting them to the same type (`2 + 2.0`).

I believe strong/weak and static/dynamic are orthogonal. And my examples are:

- Strong: `2 + “2”` is a error,

- Weak: `2 + “2”` makes 4 (or something else, see the language spec),

- Static: `var x = 2; x = “2”` is an error,

- Dynamic: `var x = 2; x = “2”` is fine.

sparkie 3 days ago

Dynamic typing can forbid the latter (at runtime), but it's implementation dependent. There's a further distinction, Latent typing, which is where types are associated with values rather than variables.

But a dynamic language can have types associated with variables, and it can forbid changing those types after their types have been checked the first time.

volemo 3 days ago

> But a dynamic language can have types associated with variables, and it can forbid changing those types after their types have been checked the first time.

So, like C++ with `auto`?

sparkie 1 day ago

`auto` is still using static typing, and is a tool for type inference. A dynamically typed version might look equivalent but would behave differently, failing at runtime rather than compile time.

hoseja 3 days ago

weak typing: 2 + "2" is 22

RUnconcerned 3 days ago

could also be "4" or 4! 4 seems like it would be the most evil option, honestly

pjc50 3 days ago

The real evil option is C: 2+"22" = 0, 4+"4" = undefined behavior and probably the value of some other variable.

sehansen 3 days ago

The real horror is "1d9" + 1 = 2, as does PHP: https://3v4l.org/Dn6Sm

manwe150 3 days ago

I think you meant: "22"+2 = "", and it is not UB to make the second pointer, only to use it

baq 3 days ago

or the most sane, depending on context... e.g. awk and perl do this.