A type (or “data type”) defines the set of possible values a piece of data might contain and the operations that are allowed on those values.
Examples of types include things like…
- “an integer”
- “a list of strings”
- “a map of string keys to floating point values”
And examples of allowed operations include things like…
- Integer -> can multiply two together, compare to see which is greater
- List -> can count the items, filter items by a condition
- Map -> can count the keys, look up a value by its key
Why bother with types?
Defining static types takes time and adds clutter to your code, so why do it?
- Type hints allow your dev tooling (a compiler or other type-checker) to warn you if you try to perform an operation on a piece of data that may not support it because of its type
- A common example of this is data trying to perform an operation on data that may or may not be empty
- By catching potential type errors errors while you code (e.g. calling a method that doesn’t exist on empty data), you can handle them and prevent them from causing issues at runtime
- Once you get used to reading them, types also act as documentation that makes it much easier to understand the data that’s being passed around when you’re learning a new codebase, reading code outside your IDE, etc.
Assigning types to data
Types can be explicitly assigned by the programmer as type hints:
value: dict[str, float] = { "first": 1.1, "second": 2.2 }
Or inferred by the language based on the context:
value = "Python will infer value is a str type"
Widening vs narrowing
(move this subtopic to separate post?)
- Not all languages encourage this notion of starting narrow (no assumptions) and gradually widening the type as assumptions are tested and proven (e.g. “unknown” -> “string”)
- Python is an example of a language that may explicitly discourage type widening
- PEP 483 – The Theory of Type Hints | peps.python.org
- My current take:
- Type narrowing is often appropriate
- e.g. this function argument could be one of two types; let’s narrow that down in the body of the function so we know what we have on each call
- But type widening is the right way to approach unknown data types
- e.g. the data was input by a user; it could be lots of things; let’s make no assumptions and validate what we expect
- Type narrowing is often appropriate
Reference
- Data type • Wikipedia 📚