Remix.run Logo
▲ antonvs 5 hours ago

Following on from the above, this statement is also incomplete at best:

> Types are groups of related values.

This is an operational perspective which misses one of the most important aspects of types: in a statically typed language, types are a way to classify terms, i.e. they're not just about values.

As an example of why this distinction matters, consider this expression: 1 + "hello". In most statically typed languages, this will be rejected at compile time. It doesn't have to be evaluated (i.e. at runtime) to be rejected - it's invalid because the types of the terms - 1 and "hello" - aren't compatible.

This compile-time rejection is only possibly because types apply to terms, and terms aren't values. This is critical to the usefulness of statically-typed languages - without this, they'd have to run the program to detect type errors.

Benjamin Pierce's book "Types and Programming Languages" has some great coverage of this topic.

▲LoganDark 4 hours ago | parent [-]

Do you mean that if types had only to do with values, you would have to evaluate the term to determine type?

Also, 1 + "hello" is "1hello", obviously. Have you never read JavaScript? :)

▲hexasquid 3 hours ago | parent [-]

I gather it goes through an intermediate coercion step where it is "1" + "hello".

What trips us up is having existing expectations for what an operator called + should do.