It basically seems equivalent (albeit somewhat nicer in a few ways, ie. scalar types are supported) to PHP's "type hinting"[0]. I will say that it is useful, in combination with unit testing, as you can pick up on problems with things slightly easier than hoping for the best in production and tailing error logs, but proper static type analysis is more useful.
For example, in our application code, we'll use a warning for some type coercion for example
if isinstance(obj,Bar):
warnings.warn('Using a Bar as a Foo!')
In production, this will just print a message to the log. So this is useful for things like phasing out old behavior, where we try to stop using it but don't want hard failure if it's used for now.But in our tests, we do want hard failure (so we can phase out the behavior as much as possible before removing the functionality). So we do something like the following:
if TEST:
warnings.filterwarning('error','Using a Bar as a Foo!',RuntimeWarning)
Which basically says "if I receive a warning matching the regex given, then raise an error instead of just logging". So if I hit that code path during tests it will fail the test.It's very useful, even if warnings is a bit too much about regex matching for my tastes
That being said, sum types would be a nice addition to this library.
> When most practitioners think of "typechecking", they typically think about proving properties about programs statically.
This is what the type theory community (e.g. Bob Harper) thinks...not anyone else from what I can tell (definitely not practicing programmers).
Typing it out on mobile didn't make it easy to do the right quote context. Here is a good essay about it all:
http://www.cl.cam.ac.uk/~srk31/research/papers/kell14in-auth...
vs. say
https://existentialtype.wordpress.com/2011/03/19/dynamic-lan...
There are other ways to do typing that might make more sense for a dynamic language than going down the expressiveness rabbit hole; e.g.
I'm not saying that types or typechecking are inherently static concepts. I'm also not saying that this library should do static typechecking—that would be an absurd demand. I only meant that the wording is misleading.
Even more misleading is author's use of the term "gradual type checking", which has a well-understood meaning: the ability to add static checks to an otherwise dynamically-typed program.
Most programmers don't think "dynamic type checking" is a misnomer, while it is true that "type checking" itself leans towards a static connotation.
I've seen gradual type checking used both ways in the literature, actually. Wiki has the term defined for dual phase:
http://en.wikipedia.org/wiki/Gradual_typing
> Gradual typing is a type system in which variables may be typed either at compile-time (which is static typing) or at run-time (which is dynamic typing), allowing software developers to choose either type paradigm as appropriate, from within a single language.
I'm sure this is just poor writing (as Siek defines it, it should be from dynamic to static), but there is enough confusion here where you might bother using the term for adding stronger dynamic type checks to an otherwise less dynamically typed language (if you admit dynamic types as types, of course). Also, a dynamically checked type signature is the first step to a statically typed one (as long as your types remain weak enough that static typing is achievable in the future). So in that sense, it is "gradual" typing, just from a meta perspective :)
You can get a statically-typed Ruby here: http://infraruby.com/live
It follows Java's type system (including generics with bounded wildcards) but it doesn't support Ruby reflection.
This is certainly useful sometimes, as it gives programs the desirable "fail fast" property. But it isn't "typechecking" as most engineers understand it. Or at least, it should be clarified that this is run-time typechecking. As such, it negatively impacts runtime performance, unlike compile-time typechecking.
This project also seems to miss the primary opportunity of run-time type checking: checking properties that are difficult to prove statically! For example, checking that a number is even, that a string is lowercase, that an index is within the bounds of an array, etc. These exotic types require a dependent type system to be checked statically, but in a dynamic environment they are trivial to verify.
Two suggestions for improvement: 1) add "sum" types (i.e., discriminated unions), and 2) let the user define their own types via lambdas, such as PrimeNumber.