From: Sam McCall Date: 2004-09-09T13:20:07+09:00 Subject: Re: [RCR] Unified type conversion framework gabriele renzi wrote: > #is! is broken, because it relies on is_a? that is not powerful enough > to really express a type, but #as (or #to.. I mean, the proposed system) > is powerful enough to expres any kind of type, including your #is! :) I think there needs to be a difference between implicit conversions (if there are any) and explicit conversions though: "hello".can_be! String # ok "hello".is! String # ok (maybe this is just an alias to #to, but i like # the clarity) 123.can_be! String # ok 123.is! String # raises It's like the current difference between to_s and to_str, which seems a bit arbitrary. I'm not too unhappy with is_a here, a way to specify an interface rather than a class would be nice, but it doesn't seem to be a big problem for me anyway. > The same is said for can_be? Sure, I could introduce a method in the > ConvTable that checks if a conversion path exists, but it won't work > for, say, > 'ciao'.as Integer > because even if a String -> Integer conversion path do exist, you still > have to check the whole string. So you end up needing #as again. Hmm, ok. Maybe def can_be? t self.as t true rescue TypeError false end >> This might even be rdoc-able, if it was at the top of a method. It'd >> certainly make the ad-hoc documentation I write today executable. > > > exacylt what I was thinking of. The IdeaSpace is limited, it seem ;) :) Type checking for parameters (along with identifier typos) is one of the few things that still irritates me, sometimes I think I'd be happier in a dynamic-except-for-function-parameters language ;-) Sam