Types of Expressions

Learn how the types of expressions can be defined and how type checking works.

Insufficient syntactic validation

Have you ever heard or read something that is syntactically correct but does not make any sense? We all have. Consider, for instance, the phrase “cute table cat.” Syntactically speaking, it is a perfectly valid English noun phrase, but the meaning makes no sense, at least in real life. In particular, combining the noun “table” with the noun “cat” to form a noun phrase might perfectly follow the syntactic rules of English grammar. Yet, they don’t seem to fit together from the semantic point of view.

This applies to programming languages as well. We can construct syntactically valid programs that are nonsensical. Consider, 1 + true, for instance. In most programming languages, it is a syntactically valid expression, formed by a binary operator + with two sub-expressions, 1 and true, as operands. Yet, adding an integer with a boolean value generally does not make sense.

Dynamic typing vs. static typing

If you look at 1 + true closely, it’s clear that there is a type mismatch in this expression. The binary operator + expects two operands of type numbers, but the right operand’s actual type is boolean. When this mis-match isn’t caught as invalid, we say that the expression does not type check.

How a programming language treats types hugely influences the expressivity of the ...