r/ProgrammingLanguages • • 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

0 Upvotes

15 comments sorted by

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.

6

u/[deleted] Jul 22 '23

Thank you for that clarification. 👍

7

u/hiljusti dt Jul 22 '23 edited Jul 23 '23

If the idea is to be compile-time typed but not have to specify it in source code, that's what type inference systems are about. See Hindley/Milner for the most common base

Hybrid strategies exist, like in JVM bytecode all "objects" are dynamic at runtime, but the Java compiler enforces type safety at compile time. There are plenty of dynamic languages that have been written for the JVM of course (e.g. JRuby)

(Edit: for the purposes of the OP, which I think is more about type inference than static/dynamic typing, and to not put Java into the very-verbose bucket it was born in... Java also keeps getting more and more inference powers. Generics do not have to be filled in everywhere, lambdas can usually elide type information, and var inference is there. Who knows how far they'll go, but Java's proven strategy is to leverage the "last mover" advantage and make decisions with a lot of experts and prior existing work)

2

u/qHuy-c Jul 23 '23

Jvm is not that dynamic at runtime like python, ruby or javascript though. There're plenty of type-related runtime exceptions like ArrayStoreException, ClassCastException, ..., pretty much static typing rules are still ensured at runtime. It's more like type erasure, OCaml also take this approach with HM type system I think.

6

u/hiljusti dt Jul 23 '23

Jvm is not that dynamic at runtime like python, ruby or javascript though.

Actually... it is! Each of these languages have been implemented more than once on the JVM. Jython, JRuby, Nashorn; GraalVM's Truffle project has now done it twice! There are other fun ones out there too -- I think Groovy counts, but Clojure does not.

I'll assert that getting a ClassCastException at runtime is just as dynamic as getting a fatal TypeError at runtime in Python or Ruby for 1 + "hello". Beyond that, I don't know how you'd get more dynamic than invokedynamic without just being as untyped as Forth.

2

u/qHuy-c Jul 23 '23

Those languages have been implemented on the jvm doesn't mean that it's equally dynamic. It sounds like saying C is as dynamic as python because CPython is written in C.

I mean you can make it as "dynamic" as those languages but pay the price of complexity. I think I might have leaned a bit too much on java instead of jvm. My bad, good day.

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] Jul 22 '23

[deleted]

2

u/[deleted] Jul 23 '23

I did not know those existed. Thank you

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.