However, such widespread use brings problems. "It was never intended for large programs," Hejlsberg says. "Apps are getting bigger and as they get bigger, they get harder to maintain."
The "type" in "dynamically typed" is a set of values upon which certain operations may be performed. For instance, the type may be integers, and the operations that can be performed on that type would include addition, subtraction, multiplication, and so on. Programs work on a broad variety of types, and problems arise when an operation is applied to a type for which it was not intended. If the type is, say, a string of names, trying to multiply them will not work.
The intent of TypeScript, an optionally typed language, is to allow developers to choose how much static typing they want to incorporate into their programs.
Traditionally, languages have been either statically or dynamically typed. In statically typed languages, such as Java or C++, a type checker runs during compilation of the program and can catch many errors in the program. It does this by looking at annotations in the program. An annotation is a form of metadata that specifies which function is called for at for a given variable; that makes it easier for the type checker to ensure a line of code can actually do what it says it wants to do. Typically, statically typed languages increase development time but are saferthat is, less apt to crashin the long run.
"The whole purpose of a static type system is to build a model of what is going to happen when it runs," Hejlsberg says. "It's almost as if you have the entire reference manual for what you're doing at your fingertips, and the compiler just looks it up for you as you go along."
No matter how they start out, programs tend to get bigger, and as they do there is more room for errors to creep in. "It's the inevitable progression of a piece of software," says Benjamin Pierce, a professor of computer and information science at the University of Pennsylvania. Many projects begin as a single person trying to write an app to do one particular task, Pierce says. "Pretty soon, you have 10 people adding things to it and making it better and better and it's doing things the original person never thought of and it's getting out of control."
Static typing makes altering the program easier, because when one item is changed, the type checker can point to all the other instances where it would need to change; in a dynamically typed program, the developer would have to hunt through all the code for those instances. It also makes possible the use of tools, such as autocomplete, that make life easier for developers.
Hejlsberg says the intent of TypeScript is to allow developers to choose just how much static typing they want to incorporate into their programs; it is what he calls an optionally typed language. "Typing was sort of a switch. It was either on or off," he says. "Now with Typescript, we've turned that switch into a dial."
Jeremy Siek, an associate professor of computer science at Indiana University, developed a similar type system called gradual typing along with Walid Taha, a professor of computer science at Rice University and at Halmstad University in Sweden. "If you don't put any type annotations anywhere, it really behaves like a dynamically typed language," Siek explains. "As you add more type annotations, more and more things start to get checked."
Gradual typing allows developers to decide what is important to them when writing a program. If they want something quick, in which they can check how pieces of the program run before the whole thing is completed, they can skew toward dynamic typing. If they need to be sure they are rooting out errorssay they are programming something critical, such as a pacemakerthey can rely more on static typing to make sure the code is safe. "There's this trade-off between safety and flexibility, and it (gradual typing) lets the programmer choose," Siek says.
Indeed, there have been solid reasons to prefer dynamic typing, says Julien Verlaguet, a software engineer at Facebook who led the development of Hack, which is supplanting PHP as the code underlying the social networking site. When the company started out, Facebook developers had relied on the dynamically typed PHP because it provided rapid feedback; they could modify code and quickly see how that changed the user experience. "The only way to test if something feels right is to interact with it," he says. With a statically typed language, they would have had to wait minutes for the code to be compiled. "The compilation time basically kills the interaction for the developer."
Facebook fixed the time lag problem by introducing HVVM, a virtual machine that acts as a fast compiler. In March, Facebook introduced Hack, a gradual typing language. In general, Verlaguet says, static typing is preferable, both because of the safety of the resulting code and the efficiency provided by the development tools it makes possible. Developers would have statically typed much of the code in the first place if they had that option, says Verlaguet. "It's just that there wasn't a type checker in place to enforce it."
If a code base gets large enough, however, there will be scenarios in which dynamic typing works better because it provides different options for defining terms, so allowing the developer to choose which to use makes sense, he says.
To provide options for Python programmers, Jukka Lehtosalo started developing mypy while he was a Ph.D. student in computer science at the University of Cambridge. "You can give it any Python code and it checks whether it's consistent with static type," Lehtosalo says.
"Basically, mypy is just Python; or, strictly speaking, a subset of Python," he says. "In mypy, you can use any Python IDE, and it works because it's valid Python."
This is not the first marriage of Python and static typing. Cython was created in 2007 as a superset of Python that allowed it to use types from C and C++. Lehtosalo says that where Cython was aimed at improving performance, mypy is more about enhancing developers' productivity and improving the quality of the code.
As a one-man project, mypy is not as far along as some other gradual type systems. Lehtosalo, whose job at Dropbox is unrelated to this work, hopes to keep moving the language from experimental to something widely useful.
Because both mypy and TypeScript remove the type annotations before running, they are giving up one method to make certain the code is more efficient. It is possible to use type information to speed up programs, says Lehtosalo, and that may happen with a future version of mypy. A language that did that, however, would be a new variant, and thus wouldn't be 100% compatible with Python.
Hejlsberg points out that, while annotations technically are not used to improve performance at runtime, the checking they provide at an earlier stage increases the likelihood the compiler will generate efficient code.
Still, languages are adding gradual typing, from both ends of the spectrum. For instance, C# 4.0, originally developed by Hejlsberg, added dynamic type to that previously static language. "I do think we are seeing a gradual breaking down of the traditional taxonomy of languages," says Hejlsberg.
"I've certainly been happy to see a lot of the newer languages come out with gradual typing," Siek says. "My hope is that more and more of them will decide to take the middle ground, or take the let-the-programmers-decide approach, and will be more gradual."
Siek, J.G., Taha, W.
Gradual Typing for Functional Languages, Scheme and Functional Programming 2006, Portland, OR.
Types and Programming Languages, MIT Press, Cambridge, MA, 2002.
Meijer, E., Drayton, P.
Static Typing Where Possible, Dynamic Typing When Needed: The End of the Cold War Between Programming Languages, OOPSLA'04 Workshop on Revival of Dynamic Languages, 2004.
Type Theory Comes of Age, CACM 53 (2), February 2010.
©2014 ACM 0001-0782/14/10
Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page. Copyright for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee. Request permission to publish from firstname.lastname@example.org or fax (212) 869-0481.
The Digital Library is published by the Association for Computing Machinery. Copyright © 2014 ACM, Inc.
Great article! I wish that it included Typed Clojure - http://typedclojure.org/
It's derived from the work done in Typed Racket, but continues to extend the approach. It can also very easily be integrated into tooling - https://cursiveclojure.com/
Displaying 1 comment