TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Gradual type checking for Ruby

60 点作者 eduardordm大约 10 年前

15 条评论

curryhoward大约 10 年前
When most practitioners think of &quot;typechecking&quot;, 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.<p>This is certainly useful sometimes, as it gives programs the desirable &quot;fail fast&quot; property. But it isn&#x27;t &quot;typechecking&quot; 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.<p>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.<p>Two suggestions for improvement: 1) add &quot;sum&quot; types (i.e., discriminated unions), and 2) let the user define their own types via lambdas, such as PrimeNumber.
评论 #9345777 未加载
评论 #9345843 未加载
评论 #9345791 未加载
Mithaldu大约 10 年前
&gt; This gem brings you advantage of type without changing existing code&#x27;s behavior.<p>I&#x27;m fairly sure it changes the performance characteristics of code it is applied on. I&#x27;d recommend adding benchmarks to the README so prospective users might be aware of this beforehand.
评论 #9346451 未加载
评论 #9345679 未加载
JoelMcCracken大约 10 年前
This is very similar to contracts.ruby, which is cool. I&#x27;d love for more contract discussions.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;contracts.ruby" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;contracts.ruby</a>
评论 #9346567 未加载
anaolykarpov大约 10 年前
I find that putting the method signature at the end of the method definition can become unreadable pretty fast. Also, the fact that it doesn&#x27;t offer any performance improvement (most probably, this will actually degrade performance) makes me see this as a cool trick, but not really recommended in production.<p>I like the aproach Perl 6 took on gradual typing. You can read about it in this article which computes fibonnaci&#x27;s number:<a href="http:&#x2F;&#x2F;blogs.perl.org&#x2F;users&#x2F;ovid&#x2F;2015&#x2F;02&#x2F;avoid-a-common-software-bug-by-using-perl-6.html" rel="nofollow">http:&#x2F;&#x2F;blogs.perl.org&#x2F;users&#x2F;ovid&#x2F;2015&#x2F;02&#x2F;avoid-a-common-soft...</a><p>The only reason I wait for the next winter to come is because Perl6 will be production ready by then as Larry Wall announced at FOSDEM this year.
评论 #9346551 未加载
cookrn大约 10 年前
I wonder if it would be possible to configure this gem with &quot;environments&quot; in the same way that Rails applications have environments. Then, in development&#x2F;test&#x2F;CI-like environments, very strict checking could be applied based on the specified types. Otherwise, in production-like environments where &quot;performance&quot; may be more important, the type-checking could be looser or simply pass-through.<p>Disclosure: this may be a Bad Idea (TM)
moe大约 10 年前
This already exists in a relatively mature form: <a href="https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;contracts.ruby" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;egonSchiele&#x2F;contracts.ruby</a>
nahiluhmot大约 10 年前
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&#x27;s some feedback:<p>* It would be better if this didn&#x27;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 versions of Ruby.<p>* Perhaps allow the user to enable&#x2F;disable type checking at a global&#x2F;class level. For example, then users could only enable type checking during specs if they wanted.<p>* Instead of using class-level variables, try using class level instance variables. They have less odd behavior when dealing with subclasses [1].<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;gogotanaka&#x2F;Rubype&#x2F;blob&#x2F;develop&#x2F;lib&#x2F;rubype.rb#L18" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gogotanaka&#x2F;Rubype&#x2F;blob&#x2F;develop&#x2F;lib&#x2F;rubype...</a><p>[1] <a href="http:&#x2F;&#x2F;www.sitepoint.com&#x2F;class-variables-a-ruby-gotcha&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.sitepoint.com&#x2F;class-variables-a-ruby-gotcha&#x2F;</a><p>Edit: Whitespace
评论 #9346070 未加载
评论 #9345824 未加载
jaggederest大约 10 年前
Reminds me of <a href="https:&#x2F;&#x2F;github.com&#x2F;lucky&#x2F;pedant" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lucky&#x2F;pedant</a> from a few years ago. I added some basic argument assertions to it as well: <a href="https:&#x2F;&#x2F;github.com&#x2F;lucky&#x2F;pedant&#x2F;pull&#x2F;1" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lucky&#x2F;pedant&#x2F;pull&#x2F;1</a>
overload119大约 10 年前
I&#x27;ve been writing Ruby for a few years on a number of production applications.<p>Recently I&#x27;ve had to pickup Hack for work, and if there&#x27;s one thing I really like about it is the type hinting. The best part is that it helps you handle nullable types (not sure if it&#x27;s done here).<p>When I switch back to Ruby from Hack, I find it harder to reason about my program.
firlefans大约 10 年前
More interesting to my mind is this (Diamondback Ruby):<p><a href="http:&#x2F;&#x2F;www.cs.umd.edu&#x2F;projects&#x2F;PL&#x2F;druby&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.cs.umd.edu&#x2F;projects&#x2F;PL&#x2F;druby&#x2F;</a><p>Some features:<p>--------------<p>Type inference:<p>DRuby uses inference to model most of Ruby’s idioms as precisely as possible without any need for programmer intervention.<p>Type annotations:<p>Methods may be given explicit type annotations with an easy to use syntax inspired by RDoc.<p>Dynamic checking:<p>When necessary, methods can be type checked at runtime, using contracts to isolate and properly blame any errant code, similar to gradual typing.<p>Metaprogramming support:<p>DRuby includes a combined static and dynamic analysis to precisely model dynamic meta-programming constructs, such as eval and method_missing.
siscia大约 10 年前
Any now and then another language get some sort of type check, why we don&#x27;t build an agnostic type checker ?<p>Then we interface with the AST of any language and we can stop re-iventing the wheel every two week...<p>It is so crazy ?<p>Nobody tried it before ?
评论 #9347872 未加载
stewbrew大约 10 年前
IMHO the state of static type checking&#x2F;code analysis in ruby is still deplorable and this (well known, rather trivial) approach won&#x27;t ameliorate the situation. Even javascript has more to offer in this respect. Who would have suspected that 5 years ago.<p>Since I still like ruby&#x27;s syntax, my hopes are that crystal (<a href="http:&#x2F;&#x2F;crystal-lang.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;crystal-lang.org&#x2F;</a>) will one day become more mainstream (and maybe be adapted to some extent in mainstream ruby).
transfire大约 10 年前
Already done many years ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;rubyworks&#x2F;platypus" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rubyworks&#x2F;platypus</a>
shitlord大约 10 年前
Is there an error in the section named &quot;Typed method can coexist with non-typed method&quot;? There&#x27;s a line that has `typesig :sum` but sum is never defined.
revskill大约 10 年前
Why Ruby 2.0 ?
评论 #9345878 未加载