From: Alexey Muranov Date: 2011-11-25T21:54:11+09:00 Subject: [ruby-core:41297] [ruby-trunk - Feature #5583] Optionally typing Issue #5583 has been updated by Alexey Muranov. Excuse me if this thread is not a good place to post this question, but i will appreciate any clarification or a link to an answer. What is Duck Typing after all, and how is it supposed to work? According to Wikipedia: "duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class" But what is the "set of methods"? If it is the set of their *names*, then what if unrelated methods of different objects happen to have the same name? Is there some written requirement about Ruby that, for example, #to_s method has to always convert an object to String? Also, if duck typing is the only typing allowed, then how would you define what String is? I have been thinking how to check the type of an object, which can be needed, for example, to implement #deep_value (#5531) or other #deep_ method for a hash, and the only nice way i could think of was to use #is_a? method. This would also work if i would want to group classes in some way by their behavior. For example, there are many classes that implement #[] method (Array, Fixnum, Hash, String), so checking if this method exists in this class does not always give adequate information about its behavior. So a natural way to group together classes that use this method to store and index objects, like Array and Hash, would be to create an empty module with a unique name, KeyValue for example, mix it into Array and Hash and any other class where #[] method behaves similarly, and to use #is_a?(KeyValue) to find out the true behavior of an object. But Wikipedia says explicitly that this will not be a Duck Typing, and that in Ruby there shall be only Duck Typing. -Alexey Muranov. ---------------------------------------- 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