From: Alexey Muranov Date: 2011-11-15T00:30:08+09:00 Subject: [ruby-core:41022] [ruby-trunk - Feature #5583] Optionally typing Issue #5583 has been updated by Alexey Muranov. Yukihiro Matsumoto wrote: > (b) in dynamic language like Ruby, optional typing should honor duck > typing, I don't think previous discussion consider this aspect > deeply enough. My understanding of how it can possibly work was the following: when `foo` method that requires a string argument is called, it first of all calls the argument's `to_s` method (raises an error if there is no such method). I think this is supposed to help catch bugs early and to make the code better documented. For typed variables, it seems that their class has to be "frozen", and no singleton methods allowed. ---------------------------------------- Feature #5583: Optionally typing http://redmine.ruby-lang.org/issues/5583 Author: Yasushi ANDO Status: Open Priority: Normal Assignee: Category: Target version: Although I know all of you dislike static typing it cannot be denied that there are some people aspire for introducing it in ruby. The dartlang solved the problem in unique way called Optionally Typing. In Dart you can declare types for variables but they are ignored by the compiler with default options. It looks superb idea for me so that I introduced the optionally typing to ruby as a trial. You can write types for variables if you want then the ruby completely ignores these dirts. Sample code: def method(arg) arg end def typed_method(arg) : String arg end def fully_typed_method(arg : String) : String arg end var = 'var' puts var var_with_type : String = 'var_with_type' puts var_with_type var_from_method : String = typed_method('var_from_method') puts var_from_method @ivar : String = fully_typed_method('@ivar') puts @ivar Results: $ ./truby -Ilib -I. truby_test.rb var var_with_type var_from_method @ivar I attached my poor patch. Thanks for your consideration. -- http://redmine.ruby-lang.org