From: "headius (Charles Nutter)" Date: 2012-04-03T02:14:15+09:00 Subject: [ruby-core:44076] [ruby-trunk - Feature #4264] General type coercion protocol for Ruby Issue #4264 has been updated by headius (Charles Nutter). Mostly I'd like to see some consistency in coercion supported by a base protocol in Ruby itself. The rough proposal was that BasicObject could have something like #convert_to(cls) that attempts to call a conversion method on the target class, passing self. class BasicObject def convert_to(cls) cls.convert(self) end end This double-dispatching protocol could allow simple conversions to be implemented in terms of Class#convert: class BasicObject def to_a convert_to(Array) end end class Array def self.convert(obj) [obj] end end And so on. But the more valuable benefit of the protocol would be that instead of having everyone add methods to BasicObject for their own conversion type, they'd add convert impls to their custom classes class MyDataType def self.convert(obj) # logic for converting arbitrary objects to MyDataType end end There's many dimensions to this discussion, of course, but I wanted to start the discussion about making Ruby's coercion protocols a bit more formal and supported by standard APIs. ---------------------------------------- Feature #4264: General type coercion protocol for Ruby https://bugs.ruby-lang.org/issues/4264#change-25610 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/