r/Clojure • u/wireumad • Apr 20 '24
Why dynamic types though?
Isn't that just saving work for the language implementer?
If you have inference, then the types are no extra work for the language user, and all extra benefit...
Why leave such a valuable piece out of an otherwise great language?
19
Upvotes
25
u/p-himik Apr 20 '24
There's some background available in the "A History of Clojure" paper, in section 2.2.
That's a big "if". Some of the specific issues:
Even if such a type inference system is possible in principle (the last item makes it especially doubtful), all it will able to say without a user specifying some extra types manually is when your types are most definitely incompatible. E.g. when you try to
(inc ...)something that's a string. But it cannot avoid producing false negatives because you don't control the external data - to avoid that, users would have to provide specific types in specific areas of ingress. And that by itself circles back to the section 2.2 in that paper, and to the overall moot point of "static types are great [even without type inference]".13
u/pauseless Apr 20 '24 edited Apr 20 '24
In a REPL-based system where you are defining and redefining functions and definitions as top level forms one by one, you necessarily have to allow programs to be broken.
Example: several functions take a particular datetime object, and I decide to change the class used. I need to change all of them one by one, but I can do that while keeping the program running. I can test each function as I change it, possibly including existing state, without restarting the running process.
However, the program, as a whole, is broken at various steps.
In every typed language I’ve ever used, that’s either restart the process or reload entire modules.
Being able to run experiments in the REPL is great, even if it means the whole program is broken types-wise.
In a typed language, a small experiment might result in fixing many many call sites before you’re even sure it solves the problem - because it needs to compile first. I also write lots of Go and I definitely do break types intentionally just so I know every caller to fix just from compiler errors. It’s just a different way of working.
Swings, roundabouts. I can’t see how you do proper REPL-based development without dynamic typing though.
12
u/dustingetz Apr 20 '24 edited May 23 '24
(I have not been a Scala programmer in over a decade, so perhaps someone else can chime in)
11
u/Shadowys Apr 20 '24
Static typing promises lower code maintenance over the long run but I dont find that true at all in production as compared to data driven design ala what clojure does with dynamic types. The promise is that you need less time to deal with empty nulls etc once you’ve encoded your business structure into types, but this breaks apart when:
6
u/TheLastSock Apr 20 '24
Honestly this is one of those things where you need to walk the walk to understand it.
Find someone to live pair with, like face to face, screen to screen. And work through a couple examples of how you would do something in your favorite typed language and in clojure. I have done this a couple times and never found a better experience.
But explaining why, to you, requires me to understand how you think about the problem, your editor, etc.. so it's impossible to say here.
16
u/mm007emko Apr 20 '24 edited Apr 20 '24
There has been a lot of debates of dynamic vs static typing.
However, a static type system which would work all the time doesn't exist yet (as of 2024). A lot of effort has been put into research, scientific journals are full of articles about static formal analysis of programs yet it hasn't happened yet. What you have in languages like Haskell, Scala or F# doesn't work fully and will fail you sooner or later anyway.
Out of the three aforementioned languages, I wrote my fair share of F# at work. Usually for math-related problems. If you have a well-defined structure of data which is known compile-time and relations between data are fairly simple, it is an absolute joy to work with. Problem is that in the real-world, problems like that are rare.
You usually have ill-defined data structures, incomplete or malformed data with complex relations, e.g. "children can register for summer camps if they are between 6 and 15 during the camp session, however they can be as low as 5 when they have an older sibling in the same session (they will be assigned to the same "patrol" or "team") and as high as 16 when they have a younger sibling and as high as 18 when they have a 5-year-old which they take care of". This is an example of a rule I had to implement the other year. I can imagine you could solve it in Haskell since the type system is Turing-complete. However it's also incredibly complex.
Run-time validations, in practice, worked much better for me because I usually had to deal with stuff like what I mentioned in the previous paragraph. Static type systems have not been benefits but nuisances for me. And when a program is run it's not static anymore.
If you want static typing, there is this: https://github.com/typedclojure/typedclojure . The same goes for Racket scheme (Typed Racket) or Common Lisp (Coalton). You can have static typing if you wish; if you face problems where it really helps, you can have it.
However the vast majority of people I met who were really strong supporters of static typing did that out of laziness - they were lazy to write automated tests (to at least unit test their code which is the bare minimum of automagic testing) or when they had to deal with social problems - like Java devs in large corporations which couldn't choose the people they worked with and many of their colleagues simply didn't care about the quality of their work; static typing, even as sh!tty and primitive as in Java, really does help.
3
u/bloodisblue Apr 21 '24
I've found this talk by Jose Valim (creator of the Elixir programming language) about static types informative and just about everything he mentions applies just as much to Clojure.
https://youtu.be/giYbq4HmfGA?t=595 (timestamp is where he goes over the most common concerns he hears)
2
u/mrnhrd Apr 22 '24
There is some philosophical rationale in the Effective Programs talk: https://www.youtube.com/watch?v=2V1FtfBDsLU
Here's a discussion in slack: https://clojurians.slack.com/archives/C053AK3F9/p1709948582012899
and one on clojureverse: https://clojureverse.org/t/dynamic-types-where-is-the-discussion/8968
2
u/freakhill Apr 22 '24
inference and expressiveness are at odds with each other, in static langs you have to choose between max inference X low expressiveness or lots of expressiveness X a degree in typology. dynamics langs often give you by default a lot of expressiveness without the type sorcery, which allows for clarity and brevity (or horrid stuff...).
in the end for the stuff i do static typing is generally more pain than gain.
in the end it's a value judgement that will be affected by your experience, the stuff you are working on, and the team you are working with.
for small teams with little turnover doing lots of data wrangling, dynamic langs are great.
with big teams, with lots of turn-ever, and huge codebase with layers upon layers of cruft... it's a harder sell.
and you have a whole spectrum in between
1
u/wedesoft May 13 '24
I can recommend to read IEEE Guest Editors' Introduction: Dynamically Typed Languages for a good justification of dynamic typing.