Live data from Hacker News

Gradual type checking for Ruby

github.com

41–43 of 43 posts

Re: Gradual type checking for Ruby

#41

Earlier quoted context omitted.

> Not sure if OP is the author The OP is eduardordm, which can be found right underneath the article link. If you look on his HN profile, his GitHub name is also eduardordm. The repo author is gogotanaka. So, no. He's not. And the repo author is consequently unlikely to ever see your comments. Not only are these simple checks to make, but it's extremely bad practice to scatter commentary about a project everywhere ac…

What are you suggesting, that there never be any discussion of code that lives in a github repo on HN, but instead all discussion take place... in the GH issues?

Yes.

Re: Gradual type checking for Ruby

#42
post #29
post #16

Earlier quoted context omitted.

One thing we've applied on a Python(2, unfortunately, so can't go deep on the static type analysis) is enable this sort of type hinting when running our app in debug or test mode. Obviously can't catch everything, but it catches some of the sillier bugs.

Would you share how you did that technically in Python? Maybe some code example or a lib to recommend? decorators? assertions?

python's warnings library is good for this

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

Re: Gradual type checking for Ruby

#43
post #21

Any now and then another language get some sort of type check, why we don't build an agnostic type checker ? Then we interface with the AST of any language and we can stop re-iventing the wheel every two week... It is so crazy ? Nobody tried it before ?

Type checking is typically done statically, i.e. during compile time. By the time you have AST it's too late. Think about it, how would you handle type annotations in an agnostic type checker?

It's definitely me being to naive, however I would type all the expression, in either the source code (into comment) or in a separate file, and all the statement.

Now I do have a table of symbols and their type.

At this point its just a matter of using the AST to being sure that everything is correctly typed...

Am I wrong ?

Post reply on HN