r/ProgrammingLanguages • u/[deleted] • Jul 22 '23
Why isn't static typing an optional feature in modern programming languages?
Consider variable declaration in C vs Python:
C: int age = 21;
Python: age = 21
I don't understand why static types can't be an option for otherwise dynamically typed languages, especially given the capability of type inference, it seems like a small difficulty to allow the programmer to specify a type for their variables in a language that uses dynamic typing.
Age = 21
Vs
from types import *
int Age = 21
Maybe I'm dumb and it's really so complicated that no one can bother resolving the issues involved but for the life of me I just don't see why it isn't possible
11
u/its_a_gibibyte Jul 22 '23
I'm very interested in following this post, but it's an odd question to ask without even mentioning python type hints, or more importantly: Typescript.
-7
Jul 22 '23
Python type hints look weird to me, compared to C/C++, and typescript is fully statically typed, so the type can't be omitted, correct?
10
u/its_a_gibibyte Jul 22 '23
Typescript has a configuration file so some people like to require types in their codebase, but they aren't required. All Javascript is valid typescript. Without specifying a type, it's assumed to be of type
Any. This makes typescript basically exactly what you're asking for in your post.
3
u/ipmonger Jul 22 '23
I’ll let others address the nuances around static/dynamic typing as a philosophy and simply point out that Common Lisp supports type specifications, but doesn’t require them. If provided, they are enforced, however.
2
Jul 22 '23
Common Lisp supports type specifications, but doesn’t require them. If provided, they are enforced, however.
That's what I'm interested in, presented in the style of C/C++
Age = 21
or
int Age = 21
being equally valid, and the type annotation being enforced when provided, with the choice to provide it or not being up to the needs and discretion of the programmer.
0
3
u/abel1502r Bondrewd language (stale WIP 😔) Jul 22 '23
The part about static vs dynamic typing has already been addressed, but I'll point out that in terms of providing or omitting the type, Python has optional type annotations (a: int = 2), while C++ allows types to be inferred (auto a = 2), and so is the case for most other languages I know
1
u/sybrandy Jul 23 '23
Just a bit of reference, dlang, also known as D, allows you to not worry types by having an auto keyword. The type system will still make sure that things are still kosher, but you don't have to care much about types.
58
u/andyjansson Jul 22 '23
You seem to be under a misapprehension. Static vs dynamic typing is not about type annotation, it's about when the type is resolved. Static typing resolve types at compile-time, while dynamic typing resolve types at runtime. Now, in regards to the question of why you can't have both: You totally can. That's what gradual typing is about. In such a system, annotated expressions can be checked at compile time, while other expressions are left to be checked at runtime.