News
Computing Applications News

Gradual Evolution

Dynamically typed languages adopt features of static typing to cope with growth.
Posted
  1. Introduction
  2. A Matter of Choice
  3. Evolution Continues
  4. Further Reading
  5. Author
  6. Figures
the continuum of development languages
The continuum of development languages.

When Brendan Eich created JavaScript over the course of several weeks in 1995, his aim was to make it easy to write small applications for Netscape Navigator 2.0, one of the early browsers for the newly emerging World Wide Web. He probably did not envision that, two decades later, the language would be one of the most widely used on the Web and that the programs written in it would have tens of thousands or even a million lines of code.

“If you want to write an app and have it run in the widest number of places and on the widest type of devices, you pretty much have to write it in JavaScript,” says Anders Hejlsberg, a Technical Fellow at Microsoft Research.

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 issue is that JavaScript is a dynamically typed language, as are several other popular programming languages such as Perl, Python, and PHP. Such languages do not require developers to define every variable type as they go, and the system checks the program for errors at runtime. That makes it easier to write and rewrite an application quickly, but it also means bugs can remain hidden for months or even years, until a particular execution of the program trips over the error and crashes. As Hejlsberg puts it, in a dynamically typed language, you do not discover the flaws while you are building the space shuttle; you find them once it is flying.

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 safer—that is, less apt to crash—in the long run.

In an attempt to combine the best of both systems, computer language experts have been busy developing static type systems that can be used along with popular dynamically typed languages. Hejlsberg’s attempt to keep his metaphorical space shuttle from blowing up after launch is called TypeScript, a superset of JavaScript. TypeScript overlays aspects of a static type system onto JavaScript to cope with the complexities of ever-larger programs. Earlier this year, Facebook launched Hack, a version of PHP that includes static typing. Typed Racket adds static typing to Racket, a dialect of Scheme, and mypy does the same for Python.

“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.

Though a programmer in an Integrated Development Environment (IDE) sees the annotations in TypeScript, they are stripped away when the program is compiled. “To the browser, it just looks like plain old JavaScript,” says Hejlsberg. TypeScript also incorporates JavaScript libraries, such as JQuery, to help make development smoother. As an open source project, it also takes advantage of developments such as DefinitelyTyped, a compilation of type definitions created by various people.

TypeScript 0.8.0 was released in October 2012, and version 1.0 came out in April of this year. Hejlsberg says he and his team are already looking toward ECMAScript 6, the upcoming version of the standards behind JavaScript, and planning to incorporate its new features.

Back to Top

A Matter of Choice

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 errors—say they are programming something critical, such as a pacemaker—they 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.

Back to Top

Evolution Continues

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.

Whether gradual typing will become the major programming paradigm remains to be seen. Google has been promoting gradual typing language Dart to replace JavaScript, but adoption has been slow. Meanwhile, in June Apple introduced Swift, which is statically typed, as a successor to Objective C.

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.”

Back to Top

Further Reading

Inside Typescript
http://bit.ly/1pW7xNX

Siek, J.G., Taha, W.
Gradual Typing for Functional Languages, Scheme and Functional Programming 2006, Portland, OR.

Pierce, B.C.
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.

Wright, A.
Type Theory Comes of Age, CACM 53 (2), February 2010.

Back to Top

Back to Top

Figures

UF1 Figure. The continuum of development languages.

Back to top

Join the Discussion (0)

Become a Member or Sign In to Post a Comment

The Latest from CACM

Shape the Future of Computing

ACM encourages its members to take a direct hand in shaping the future of the association. There are more ways than ever to get involved.

Get Involved

Communications of the ACM (CACM) is now a fully Open Access publication.

By opening CACM to the world, we hope to increase engagement among the broader computer science community and encourage non-members to discover the rich resources ACM has to offer.

Learn More