Why don't I encounter more type errors when programming in Clojure?
This is an episode of The Eric Normand Podcast.
Subscribe: RSSApple PodcastsGoogle PlayOvercast
I give another reason why I don't encounter so many type errors in Clojure.
[00:00:00] Eric Normand: Why don't I encounter more type errors when programming in Clojure?
[00:00:07] Hello, my name is Eric Normand, and this is my podcast. Welcome.
[00:00:13] So I was recently on the podcast called Software Unscripted. It's a great podcast hosted by Richard Feldman. Great advocate for functional programming. He's actually spoken at a Clojure conference. We had a great conversation and the topic was: Why don't I encounter more type errors when I'm programming in Clojure?
[00:01:18] And we go through a bunch of reasons. I don't even remember everything I said, but I do remember something I didn't say and I'm gonna talk about that now. So go listen to that episode. Go subscribe. It's a great podcast. But the thing I didn't say, because I forgot, it just didn't come to mind, is that typed languages make things into type errors that we don't, that we consider them something else.
[00:01:56] Maybe it's a value error or, or something even as mundane as I got the argument order wrong. So let's say we have a function that takes two arguments, a string and an integer, just to make it simple. And when I'm programming, oh, I'm using that, I'm calling that function somewhere, but I forget which comes first, the string or the integer.
[00:02:27] And I get it wrong. And I put the integer first, and I put the string second. Now if I was in Haskell: type error, clear type error. This is supposed to be a string. You passed in an integer. This is supposed to be an integer. You passed in a string. But if I do that in Clojure, I don't think, oh, a type error. No. I think I got the argument order wrong,
[00:02:57] This is something that a Haskell programmer would say, that's a type error. And you know it is. It's the wrong types, right? But really the root problem is I got the order of the arguments wrong. I might be encountering these errors all the time, but I don't count them in the bucket of type errors.
[00:03:22] I think it's a simple explanation for why I don't experience myself encountering all these type errors all the time, because in Clojure, and most dynamically typed languages, we don't consider that kind of thing a type error. The root cause is not, I got the type wrong, the root cause is argument order or whatever it might be, right?
[00:03:52] Something like I added too many arguments or too few arguments. In Haskell, that's a type error. In Clojure, it's not. It's just, I forgot an argument or I added an extra one.
[00:04:09] Haskell has a very fantastic, powerful type system, and it's like a form of static analysis. It can prevent errors. If you make more problems into type problems, then you're gonna be able to take advantage of this powerful type checker, this inference engine that's in there, and that's what it's done.
[00:04:37] But the consequence of that is that it molds the way you view errors, that you start to think of them as type errors when that's not the root cause. And so what a Haskell programmer considers a type error and what a Clojure programmer considers a type error, they could be very different sets, some overlap, obviously.
[00:05:09] But there's this whole class of errors that Haskell turns into type errors that when I'm programming in Clojure, I'm just not thinking, oh, I found a type error. Like, oh, that null pointer exception. That probably wouldn't have happened if I had a good type system. So that must be a type error. No, that's not on my mind.
[00:05:33] I just wanted to bring that up because I forgot to mention it and I should have said this more on the podcast. I'm not saying that that makes un typed languages better or anything like that. I'm just trying to answer the question of why it seems that I don't encounter so many type errors in Clojure, and one of the reasons is, the one I've talked about today, I do encounter errors. I just don't consider them type errors. That it's kind of a side effect of the way Haskell has turned a lot of programming errors, like a typo in the name of a field, into type errors.
[00:06:23] I transposed a T and an R in the name of a field, and now I'm getting weird results. Things are failing when they shouldn't be. And lo and behold, I find that typo and now it's fixed. I don't consider that a type error when I'm coding in Clojure. It's a typo. It's a spelling error. Of course in Haskell, that would've been caught by the type checker. And so the Haskell person might call that a type error. Haskell does type errors.
[00:07:03] This has been another episode of the Eric Normand podcast. Thank you for listening, and as always, rock on!