 Welcome to my talk gradual typing is the best. This is where I was gonna call out everyone who thought this wasn't gonna work But it kind of isn't working, so We're gonna skip that part of my talk My name is Cameron Matheson. I'm a software engineer at Instructure and You probably shouldn't listen to me because I'm talking about gradually type languages, but I spend all my time writing a Dynamically type. Thank you dynamically typed code and Ruby and JavaScript. I did do half a Haskell MOOC once though So I like to pretend I'm an expert So I wanted to start by I listen to a lot of podcasts and I heard one recently about this family that was way into like the Christmas spirit specifically they really were into Santa Claus and they would Staged these like elaborate real-life encounters with Santa like their kids would stumble upon Santa in the forest and they'd have to help him out somehow I give his reindeer some carrots or something and This worked out great for most of their kids but one of their sons like really took the Santa Claus thing to heart and So much so that in high school. He's getting like fistfights with other kids to defend the reality of Santa Claus By the way, I hope I'm not spoiling anything for anyone but you know the school administration had to call his parents in to Try to you know have the Santa talk with this this high schooler And they were willing to do that in fact even on the radio. The dad was unwilling to refute the existence of Santa Claus so unfortunately a Careless relative once let slip that he was the Santa in one of these encounters and this kid's life was ruined immediately like His world fell apart 15 years later. He still can't have relationships with people or trust anyone And the reason I'm telling you the story is because like I had this exact same experience Except it wasn't with Santa Claus It was it was several years ago I was a student and I was super happy, you know as I was writing a lot of ruby which I loved I was doing some scheme at school, which I really liked It's doing a little bit of JavaScript. Oh back then I think it was called jQuery and So, you know, it looked like dynamic I started programming with C++ and you know, it was hard back then it was the dial-up days So it didn't have stack overflow or anything and things were confusing Java was verbose It looked to me like the industry was moving towards dynamic languages, you know Facebook was on PHP a bunch of the startups were doing rails life was good and Then I took a course at school I was way into compilers and programming languages at the time So I took this class called programming languages and semantics and in that class we just did by hand interpretation of Lambda calculus programs basically using different like machine types and in a huge portion of the class was actually dedicated to Static type systems, which surprised me because I thought they were going away But it turns out that you know the past couple decades Programming language researchers had been developing like more and more sophisticated type systems. The industry was totally ignoring You know, you're getting stuff like Miranda and Haskell and dependent types. All this really cool stuff that could Verify correctness of a lot of properties of your program before it runs and so Static typing was Causing some like problems in my brain and my heart because there are a lot of benefits to it, you know, it Could catch errors early. I hate writing unit tests And I think like a lot of the unit tests are right or bull crap that could be caught by a static type checker So that was appealing Tooling is better with statically type languages. You can pry them for my cold dead hands, you know but there are like quiet moments of introspection where I am secretly jealous of like Java IDs, you know, they do like this sophisticated refactoring and Accurate auto-complete stuff like that is it's cool And documentation, you know for my for my half a Haskell MOOC I did learn that given a method name and a type signature You basically know what stuff does How many times are you on like MPM and you pull down some undocumented modules? You got digging to the source and some function you a call takes like a oh, and you're like what the crap is that? So static typing is great and So this is where like I was in crisis mode, you know, I had been so happy and now like I hated programming because Yeah, you know dynamic languages are clearly inferior So that's when my Haskell experimentation began and Quickly I discovered that that was a failed experiment Haskell has these like it has a really sophisticated type system and it's lazy and those two things combine into these horrible type errors In fact like when I would learn about type systems at school You would the things professors or other people on the internet often like to say no is like once your programs type checks It's gonna run correctly and I thought that was a selling point, but it really just means your program never runs So the Haskell people figured this out I think you can defer type errors to run time now, but it was awful Also, you know the writing was on the wall JavaScript is gonna be more and more important for us web people, you know And so I knew that I was gonna have to be in JavaScript anyway Additionally static typing has you know, like the way studies there's trade-offs, right? Like assuming this room is the universe of valid programs Static types would be like one column of chairs or type checkable programs, right? So a lot of it rules out a lot of valid programs and a real-life example of that would be like Recently there was closure transducers or transducers transducers JS. Those don't have like a real type signature So you're you're limiting yourself in some ways So then Programming leaders researchers researcher they they saved the day. This is where I have to do a disclaimer because I don't actually, you know, I don't have a PhD I'm gonna mispronounce a bunch of names leaving probably some important stuff out and Others, you know take this with a grain of salt by right a bunch of papers in the mid-2000s Gillian Bracca, I'd said I was gonna mispronounce these things This is the guy behind dark. He came out with a paper called pluggable type systems and in this paper he was arguing that Static type system shouldn't be a language feature right languages should take Type systems as plug-ins so you could have one or many type systems and he explored this in a programming language I think called new speak So maybe you would have your static type system type language and also, you know various linter type languages and So so that was cool, you know, it kind of like was not orthodox at the time in 2006 Eric Meyer and Peter Drayton came out with a paper called static typing where possible Dynamic typing when needed the end of the Cold War between programming languages Eric Meyer is old Haskell guy who went on to work on a lot of cool stuff in C sharp like link and the Async await stuff we heard about earlier this morning So basically their paper was arguing that Developing new stuff in statically type languages is restrictive and annoying But as your program matures and as you add more people to your team, you know, you don't want like Your dumb co-worker ruining things so Types Protect you from this So so he didn't put forth the solution, you know, they were just putting this out there, you know Like static and dynamic is it's not a good argument to make Now in 2006, this is a big one Jeremy seek and walled Taha come out with the paper gradual typing for functional languages This is where the term gradual typing is invented And so in this paper they take this strictly typed simply typed lambda calculus and Regular a lambda calculus and they develop a way that the two can interoperate And they come up with a soundness theorem which the soundness for this kind of thing means that if There was going to be a type error in one of these hybrid programs that are typed and untyped the Error is guaranteed to come from the dynamic code And so that's important because that means as you start annotating your program with types Those portions are now safe And this is done with just annotations and also inserting runtime checks That same year independently Sam's from Twitter, I don't know how to say his real name, so I'm not gonna try and Felicin come out with a paper called inter-language migration from scripts to programs This is the birth of type track it which is the first Concrete gradually typed language and it's also still the best gradually typed language It's the only one that can use the type system to inform the VM how to Use types to increase performance good heavens, okay, and So If you're interested in gradual typing and I recommend checking out type track it. It's really cool Now fast-forward six years Microsoft gives us type script, which is a type super set of java script And so we're gonna take a look at oh, it's not sound it doesn't meet all the criteria that Jeremy seek put out in his paper But it's close enough and if you're interested in where the sound is breaks down They have a page on their website that explains it really it's reasonable trade-offs So we're gonna do some live coding now. Oh my gosh, I was supposed to say cross your fingers, but I've got some terminal plug in Okay So This is a type script program also a javascript program and You know, we're gonna have an add one function that adds one to a number and Are we are we cut off? Oh Let's just do this Okay Can we see this good So if I run this, you know, oh first let's type check I've got this Vim plugin that lets me type make and nothing comes back. So we type check Which we would expect because we have no type annotations. And so if we run the javascript 211 so that's clearly not what I meant right javascript takes strings or numbers with additions So we'll just say we want a number And now we get the suite our Argument of type string is not assignable to parameter of type number. That gives me a line in a column And it Vim even jumps me right there. Does this okay? So so this is great. It found a type error for us. I actually do run into this number and string stuff in real life So I think that's pretty cool But let's look at a harder one I instruct sure I work on a lot of assignment and homework submission type stuff so Here we're going to define an interface called submission and this this is just saying that For an object to satisfy this interface It's got to have a number score and a string property called type And then we're also going to make an assignment called class It'll have points possible a list of submissions It's got this constructor and Average score method So this just computes an average, you know, it's pretty basic stuff So let's make a new assignment. We're not passing in any submissions and we'll run average score. So first we compile Type check so it should be correct Not a number okay, so we have a divide by zero The type system can't catch this kind of errors So let's let's give it a submission though Let's see. We need a score and a Type so I said type Okay, we'll run it again. Oh comma expected Thanks. Thanks guys. This is what I have you here for Oh No Thank you New this is a mistake. Okay Okay, so works So let's add another one let's let's have two assignments, you know that way the average is actually meaningful Score five Type I know guys I'm doing on purpose this time shoot Okay, so I'm like JavaScript type script enforces that you Provide the parameters you expect if you Want optional parameters you can but by default, you know, you need to do the right thing so we'll put this in the array and Run again Great and just to demonstrate one other thing We could add another one This time we'll forget our type type or our type thing and it gives us a nice error again property type is missing So if we wanted to not require that one, we actually can make them optional with a question mark And then we type check so anyway, I think this is pretty cool My biggest Complaint with type script is just that it doesn't have anything to protect us from nulls Or like empty lists, you know, but overall it can catch a lot of errors and it's great Now last year Facebook released flow, which is another JS type checker It's made for huge code bases. So it's it's really fast. It has a demon that runs in the background and Just updates the things that actually changed so that's cool The really odd I audacious part though as they added type inference, which as far as I know has never been tried in a gradual language and I don't think it's possible to do it like completely correctly, but We'll see that it does pretty awesome. So here we have a flow program There's the square function x times x and we're gonna square to so we'll flow check it That works now, we'll try to square the string to You know in type script this would type check because we haven't annotated square, right? But in flow. Oh We get the sweet air string. This type is incompatible So that's pretty cool Now add one this is our example from earlier, right? And here we're gonna add one to one and add one to a string It can't catch the air here because that's actually a valid operation So we would still annotate with flow and it I don't know if I mentioned this it keeps type script syntax, which is very handy Okay, so there we go Now this is this is where it addresses my big complaint with type script, which is No protection, but we'll we'll go through the example first. So food takes an x which logs the property bar And then we have three things here. We've got a which has a bar B is undefined C we mistyped bar into Baz so let's Flow check Okay, that works and we can run it although I don't know that's very interesting Okay, great Now we have foo B So type script again would have no problems here Okay, this is kind of amazing to me property bar cannot be accessed on possibly undefined value so When we ran foo before there were no issues But now that there's a potential of running foo with a undefined x It makes us actually check the x exists and now we should type check Great and let's try see also. So this one has the typo, right? Okay, zero errors. This is too bad It doesn't know that I mean flow. This is a trade-off. They've made right They don't want to make you protect against every property access ever So it won't catch like typos on your property names If you wanted protection here, you would just make an interface like we made earlier So that's very cool now recently in the past couple weeks Google went to the Yes ECMAScript dudes and proposed sound script Sound script Okay This is a shell script guys sound script is a Sound like it's actually sound unlike type script and flow it is a sound gradually type system for JavaScript That they want to be part of the next revision of JavaScript And I think like it's been met with like a whole lot of skepticism basically although Jafar would know better but I did pull up the ES six meeting notes and Sam said Unsound type systems are not usable for performance if we want types to help the VM It is not going to happen as an evolutionary type system on top of type script or flow And an ARB I think is one of the Google guys, but I don't really know He points out that yeah to get predictable performance. We need types and Particularly sound types so so you know that's that's something being tossed around In Conclusion, you know, I used to like like I would actually lose sleep at night thinking about the static and dynamic thing But turns out it's not the way to think about things. We've got gradual typing now. It's better than both It's the future and I think you should try type script or flow That's it