Issue #5583 has been updated by Alexey Muranov.


Belhorma Bendebiche wrote:
> I don't think that duck typing is an issue with optional static typing.
> 
> 1) It doesn't happen at runtime

A clash between static and dynamic typing can be observed like this (i think there can be more realistic cases):

  def foo(bar -> String) -> String
    ...
  end

  def random_class
    if it_is_raining_now?
      String
    else
      Array
    end
  end

  x = random_class.new
  foo(x)

So it seems to me that static typing has to be checked in runtime, and the benefits will be:
1. better documentation of the code,
2. errors will be raised earlier (at the moment of calling foo in the above example) or will be raised in cases where they wouldn't be raised at all otherwise, and could lead to subtle errors.

Maybe i do not use terminology correctly and it should not be called static typing in such case.

I propose instead of passing a flag to the interpreter, to do

  require 'types'

in the source, which would mix in some predefined `Type::` modules into standard classes, in would add hooks for checking for their presence with #is_a?(Type::...) when a type declaration is present.

-Alexey.

----------------------------------------
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