Issue #5583 has been updated by Belhorma Bendebiche.


I don't think that duck typing is an issue with optional static typing.

1) It doesn't happen at runtime
2) It only happens if you pass a flag to the interpreter

1) is obvious, the whole point of static typing is that your program doesn't blow up at runtime, so raising errors based on it is pointless.
And with 2), the worst case scenario is you get a warning if you're looking for one. Either way, duck typing works as usual.

This may sound weird but it works out very well in practice (and I have enough practice with AS3 to be able to tell). Even if you strictly type an API, a user can still pass whatever arguments they fancy if they choose to ignore the types, so it's a win-win.

As for syntax:

def foo(bar -> String, baz -> *) -> String

end

Where * or some other token is used to explicitly state dynamic type. It's the same as not providing a type, but in my experience it helps to document it explicitly.
----------------------------------------
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