Live data from Hacker News

Gradual type checking for Ruby

github.com

31–40 of 43 posts

Re: Gradual type checking for Ruby

#31

This is very similar to contracts.ruby, which is cool. I'd love for more contract discussions. https://github.com/egonSchiele/contracts.ruby

Personally I don't get contracts when they aren't used for static verification.

It's much easier to use defensive coding techniques at runtime without having yet another framework in there that anyone who wants to read the code has to learn. Typing a few lines less is not really a benefit when it basically increases code complexity.

Re: Gradual type checking for Ruby

#32

Really cool idea! I like that you can specify a `#respond_to?` constraint instead of a class. Not sure if OP is the author, but here's some feedback: * It would be better if this didn't pollute the global namespace by defining `#typesig` in `Module` [0] -- perhaps consider refactoring that method into a module which the user may extend. Doing so would also get you out of needing to define `Module#prepend` for older v…

> 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?

Re: Gradual type checking for Ruby

#34
post #31

This is very similar to contracts.ruby, which is cool. I'd love for more contract discussions. https://github.com/egonSchiele/contracts.ruby

Personally I don't get contracts when they aren't used for static verification. It's much easier to use defensive coding techniques at runtime without having yet another framework in there that anyone who wants to read the code has to learn. Typing a few lines less is not really a benefit when it basically increases code complexity.

The Contracts gem is very easy to use. I introduced it at work recently, and everyone got the hang of it in about 20 minutes.

A lot of languages (Java, C#, Go, etc.) have very inexpressive type systems, and that tends to make many dynamic language developers wary of them. Contracts has a very expressive type system though. It understands duck typing, adhoc union types, and a bunch of other stuff that makes it a great fit for a dynamic language.

Plus, we've disabled it in production, so we get coverage in test/development mode, but no performance penalty in prod. I look at it as an extended form of testing. Type contracts are very quick and easy to add, and they help tests catch additional errors. They're also useful in code reviews. Sometimes I'll have to search for 5 or 10 minutes to figure out what kind of thing is getting passed in, where the contract makes it perfectly clear.

The best part is that the contracts are optional. If some method takes wildly differing arguments and it's going to be a pain to give it a contract, then don't.

This is the best tool I've added to my Ruby arsenal in some time. I strongly recommend taking it for a spin.

Re: Gradual type checking for Ruby

#35
IMHO the state of static type checking/code analysis in ruby is still deplorable and this (well known, rather trivial) approach won't ameliorate the situation. Even javascript has more to offer in this respect. Who would have suspected that 5 years ago.

Since I still like ruby's syntax, my hopes are that crystal (http://crystal-lang.org/) will one day become more mainstream (and maybe be adapted to some extent in mainstream ruby).

Re: Gradual type checking for Ruby

#36
post #31

Earlier quoted context omitted.

Personally I don't get contracts when they aren't used for static verification. It's much easier to use defensive coding techniques at runtime without having yet another framework in there that anyone who wants to read the code has to learn. Typing a few lines less is not really a benefit when it basically increases code complexity.

The Contracts gem is very easy to use. I introduced it at work recently, and everyone got the hang of it in about 20 minutes. A lot of languages (Java, C#, Go, etc.) have very inexpressive type systems, and that tends to make many dynamic language developers wary of them. Contracts has a very expressive type system though. It understands duck typing, adhoc union types, and a bunch of other stuff that makes it a great…

I think you misunderstood me.

What I meant was that contracts in a language like C# give me the ability to statically verify the program at compile time. It is not a "nicer way to add conditional testing logic" but a way to formally prove correctness in my programs.

Re: Gradual type checking for Ruby

#37
post #11

Earlier quoted context omitted.

> When most practitioners think of "typechecking", they typically think about proving properties about programs statically. This project seems equivalent to adding a runtime check at each call site to ensure the arguments and return values are the correct type. 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,…

editor support for php type hinting and phpdoc method comments goes a long way. intellij can do significant refactors and and searching. you'll get real time feedback for violating types. it's not quite the same as a explicit compile step but pretty close

Komodo IDE helps with that, but I personally switched to HHVM and have a simple bash script that swaps the `<?php` for `<?hh` and back, so that `hh_client` can statically tell me about problems, even when I'm not using any of the Hack features. Works a treat, to be honest!

Re: Gradual type checking for Ruby

#39
post #36

Earlier quoted context omitted.

The Contracts gem is very easy to use. I introduced it at work recently, and everyone got the hang of it in about 20 minutes. A lot of languages (Java, C#, Go, etc.) have very inexpressive type systems, and that tends to make many dynamic language developers wary of them. Contracts has a very expressive type system though. It understands duck typing, adhoc union types, and a bunch of other stuff that makes it a great…

I think you misunderstood me. What I meant was that contracts in a language like C# give me the ability to statically verify the program at compile time. It is not a "nicer way to add conditional testing logic" but a way to formally prove correctness in my programs.

IIUC, contracts are specifically for creating verifications at runtime.

In some cases, sanity checking can happen up front/at compile time. In other cases, these must occur at runtime.

The Pragmatic Programmer has a whole section on contracts; it is pretty good.

Re: Gradual type checking for Ruby

#40
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?

Post reply on HN