News for hackers. Stuff that matters.
Re:What to Know Before Debating Type Systems (2008) by msla on Friday May 03, 2019 @05:24PM (#19822425)
> Is JavaScript not considered to be at least partially on the weak side of the spectrum? Things like using the `+` operator on arrays and objects and getting back numbers and integers seems to be to be weak typing.

I might be in the minority on this, but I draw a distinction between stuff which gives odd results but is defined and controlled, and stuff which is unspecified and only 'defined' to the extent the machine does... something when you do it.

Everything Javascript does is in the first camp. Autoconversion is specified, it's part of the language design, and the implementation handles it. What C does when you use casts to turn a string of chars to a double and add the result to some other value is undefined, and whatever the hardware happens to do is what happens.

So doing a lot of autoconversion to the point you can feed any value to any function and no errors happen greatly reduces the value of dynamic typing in making testing easier. However, the implementation is still keeping track of types, because it's doing the autoconversions for you.

The Javascript equivalent to Bliss is using stringly typing to the extent your only language-level datatype is the string, and every other type only exists in the programmer's head. But that's not a property of the language as much as it's a property of the specific codebase.

Re:What to Know Before Debating Type Systems (2008) by DonaldPShimoda on Friday May 03, 2019 @05:48PM (#19822597)
> I draw a distinction between stuff which gives odd results but is defined and controlled, and stuff which is unspecified and only 'defined' to the extent the machine does... something when you do it.

Whoa, this is totally orthogonal to the strong/weak typing distinction.

Traditionally, strong/weak typing refers to how easy it is to "trick" the type system into doing something it wouldn't otherwise want to do. C is weakly typed because you can cast things all over the place and do whatever you want with the actual values, regardless of what type they're "supposed" to have. This is completely unrelated to undefined behavior.

Re:What to Know Before Debating Type Systems (2008) by msla on Friday May 03, 2019 @07:00PM (#19823021)
> Traditionally, strong/weak typing refers to how easy it is to "trick" the type system into doing something it wouldn't otherwise want to do.

Right, and autoconversion according to spec is something the type system does want to do. It uses type information to do it, in fact.

Re:What to Know Before Debating Type Systems (2008) by DonaldPShimoda on Friday May 03, 2019 @07:07PM (#19823057)
I feel like I'm not explaining myself well haha.

The C type system is perfectly happy with casts. The behavior of casting is well specified. And yet the fact that the type system supports casts to the extent that it does means that C's type system is weak.

But this is not the only metric to judge a type system's "strength", and it was poor word choice on my part that I phrased my statement in a way that made it seem so.

JS's type system is weak because of implicit coercion. The fact that values of type X can be coerced into values of type Y at runtime without any notification to the user is indicative of a weak type system, regardless of how well-specified this behavior is. Weak type systems do not lack specification; they lack a feeling of types being "rigid" or "strong". If your values can be converted to different (semantically incompatible) types, then your type system is weak because it isn't telling you anything.

A strong type system is one which stands up to you and says "No, I won't let you do that because I don't think that's what you meant to do." A weak type system is one which does its best to let you do whatever you want. Again, the specification of the behavior of various operations within the type system (such as casting or coercion) is not the determining factor in assessing the "strength" of the type system.

Re:What to Know Before Debating Type Systems (2008) by dllthomas on Friday May 03, 2019 @07:17PM (#19823108)
> A weak type system is one which does its best to let you do whatever you want.

... or plain doesn't care?

Re:What to Know Before Debating Type Systems (2008) by DonaldPShimoda on Friday May 03, 2019 @07:22PM (#19823136)
Maybe? I think that phrasing is lacking in some clarity. Does Python care? Most people would say "no", but Python is strongly typed because you simply cannot use values with the wrong attributes. (The nominal types don't matter in Python, generally, but the structural types do.)
Re:What to Know Before Debating Type Systems (2008) by dllthomas on Saturday May 04, 2019 @12:19AM (#19824406)
Python says no in a (sufficiently) sensible way at runtime to certain classes of things, and tries to let you do what you want about others. I was referring to things like undefined behavior in C as instances where the language "plain doesn't care" what you want.
Re:What to Know Before Debating Type Systems (2008) by DonaldPShimoda on Saturday May 04, 2019 @01:44AM (#19824697)
I don't think UB has any particular bearing on a conversation about the "strength" of a type system, actually. There are plenty of non-type-related UBs, and those UBs that are related to types do not necessarily make the type system weaker. C's type system is weak due to things like explicit casting that can circumvent any type safety, for example.

Python does no runtime checks of types. The type system employed by Python is effectively a structural (not nominal) system. So long as you provide things with the right structure (i.e., your objects provide the correct fields/methods), everything will work. But many people would like say that Python just "doesn't care" what you want because of the lack of any nominal type checking at any point.

Re:What to Know Before Debating Type Systems (2008) by dllthomas on Saturday May 04, 2019 @12:07PM (#19827301)
Any UB is type-related, in that "anything can happen" destroys type safety regardless of what definition of type safety we're using.
Re:What to Know Before Debating Type Systems (2008) by DonaldPShimoda on Saturday May 04, 2019 @01:33PM (#19827890)
UB does not mean "anything can happen".

UB just means that the language specification makes no assertion about how to handle a given case — the behavior is literally just not defined. Any implementation of the language could choose to raise an error for such cases and that would conform correctly to the standard.

Re:What to Know Before Debating Type Systems (2008) by dllthomas on Sunday May 05, 2019 @02:14PM (#19834103)
Note that I specifically said "undefined behavior in C", where it pretty much means exactly that. It is not the same as "implementation defined". It's true that an implementation could in theory respect types even in the case of undefined behavior, but it is not promised by the language spec.