Static Typing for Ruby
Last week I went to a presentation by Jeff Foster about Diamondback Ruby (DRuby), a static type inferencer for Ruby. DRuby's goals are to be simple to use, be flexible enough to handle common ruby idioms, be reasonably useful, and to revert to run-time type checking as needed.
DRuby features its own GLR parser for Ruby grammar with extensions for type annotations and is written in OCaml. Internally it uses a normalized and simplified ruby subset to perform the type analysis in order to remove ambiguities and simplify flow analysis.
Type inference is fed a prebuilt set of type annotations derived from the core library that look very similar to rdoc. The annotation system can handle variable arguments to methods, type intersections and type unions. Basically, this means it's expressive enough to handle String#slice which can be called six different ways.
For details on the type inferencing itself, you should read the paper. There are currently some limitations in DRuby, among them are unsuitability for large applications due to time constraints and too many false positives, it flattens namespaces such that A::C and B::C are considered the same, and it doesn't handle #eval.
On a set of eight benchmark applications, five required some minor modifications for DRuby to operate (described in the paper), and three of the eight had false positives. I found two of the false positives interesting, as I think the code highlighted by DRuby is questionably written.
The false positive in sudokosolver is from a method that returns an Array or false then performs a run-time type check to do the appropriate thing with the results. The false positive in ObjectGraph involves a String that is duck-typed into a StringIO if StringIO wasn't required through a singleton class.
Compared to other static typing add-ons for Ruby, this is the most promising one I've seen. There's no type annotations required to muck up my existing applications (they would be provided with DRuby), no penalties for checking at run-time, and it's smart about functions as convoluted as String#slice.
Unfortunately, since it hasn't been released into the wild yet (the target date we were given was sometime this summer) it's hard to say if it how useful it would be. Judging from the two false positives given, it may have some utility as an equivalent to a flog, though. comments
Comments are disabled