Issue #5583 has been updated by alexeymuranov (Alexey Muranov).


Maybe, as a form of typing, Ruby can somehow implement *contracts*, like in http://en.wikipedia.org/wiki/Design_by_contract ?
Some "contract description language" would need to be invented, and classes or methods would then have a possibility to declare contracts they fulfill:

method_contract C
  this method returns an UTF-8 string
end

class_contract B
  ...
end

class T
  satisfy :to_s => C
  satisfy B

  def to_s
    ...
  end
end

Then other methods would be able to ask a class if it fulfills a given contract.  This would make more sense than simply asking a class if its instances respond to a method with a given name.

Also this could me useful for automatic testing.

I do not have any clear idea.
----------------------------------------
Feature #5583: Optionally typing
https://bugs.ruby-lang.org/issues/5583#change-28574

Author: technohippy (Yasushi ANDO)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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://bugs.ruby-lang.org/