Issue #4264 has been updated by trans (Thomas Sawyer).


I really don't see how this can be about the "light" conversions, to_a, to_i, to_s, etc. It only makes sense to me as being for the strict conversions to_ary, to_int, to_str. Why? Because many different methods can result in array, integer, or string, etc. These common method names are just terms used to produce a common or typical form.

Also the use of `case` statement in the double dispatch is not a good idea. We need to be able to define these per class. Maybe something like:

  class Array
    define_conversion(Foo) do |obj|
      ...converting Foo to Array...
    end

    define_conversion(Bar) do |obj|
      ...converting Bar to Array...
    end

    define_conversion(Baz) do |obj|
       ...converting Baz to Array...
    end
  end


----------------------------------------
Feature #4264: General type coercion protocol for Ruby
https://bugs.ruby-lang.org/issues/4264#change-25625

Author: headius (Charles Nutter)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 


=begin
 Justification: Ruby objects variously define to_ary, to_int and others for either explicit (before call) or implicit (during call) coercion. However, the set of methods is limited, and adding more names just clutters namespaces and isn't feasible long-term anyway. This proposal will hopefully start a discussion toward adding a general type-coercion protocol via a #to or #to_any method. To blunt the naming discussion a bit, I will refer to it as #to_x.
 
 Description: The #to_x method will be a "supermethod" of sorts that can be used to coerce a given object to an arbitrary type. Where currently there are specific methods for coercing to specific types (to_ary, to_str), and other more general methods intended not for coercion but for explicitly re-structuring an object's data (to_a, to_s), there's no one protocol for doing general coercion. #to_x would fill the roles of the coercion methods, accepting a target class and responding appropriately.
 
 The response will depend on whether the target object can be coerced to the given type. The result for success should obviously be an instance of the target type. The result for failure could either be "soft": returning nil, or "hard": raising an error. There could also be an optional boolean flag that specifies hard or soft.
 
 Existing coercion methods could (but need not be) implemented in terms of #to_x
 
 def to_ary
   to_x(Array)
 end
 
 def to_str
   to_x(String)
 end
 
 Prior art: JRuby supports coercing Ruby objects to arbitrary Java types in this way. Currently only a set of hard-coded target types are supported for various core Ruby classes, but this is intended to eventually be part of the invocation protocol when calling Java. In other words, if the object being passed is not the exact type of the target parameter, JRuby will invoke to_java(target_param_type) to do the coercion. Performance implications in this are obvious...so there may need to be discussions about modifying this protocol to make it easier to optimize.
=end



-- 
http://bugs.ruby-lang.org/