Robert Klemme
1/31/2008 10:35:00 PM
On 31.01.2008 20:22, Tim Connor wrote:
> I had a strange idea this morning, namely, how would I merge the idea
> of the contractual nature of static type safety, with the flexibility
> of duck-typing. I know, it's a hot button, and I've read various
> other threads that come close, but don't quite address it the same
> way.
>
> And I am not seriously proposing this as a "good idea" or anything. I
> was just thinking "hmm, is there any way to get ducktype safety,
> without manually throwing ParameterErrors in the header of the method
> if something fails a responds_to".
I tend to believe that this approach is at least ill named - if not
worse and here's why: If you test with respond_to? you are no longer
duck typing. If all you test is respond_to? then you gain nothing over
normal interpreter execution, exceptions are just thrown a bit earlier -
but at the price of more complex code or method declarations and runtime
overhead. Also, it's not _static_ type safety.
> Yes, there are times you want more
> complex handling if something doesn't quack, but sometimes you do want
> to enforce that, and right now it is just a bunch of extra ifs and
> unlesses.
Well, the interpreter will enforce it if methods are called that do not
exist on those objects.
> And I know this doesn't fit with he already complex enough method
> declarations in ruby.
Um, where is declaring methods in Ruby complex? I haven't noticed so far.
> It's just faux ruby style, because that is my
> favorite dynamic language. So, without further prevarication, here is
> the aforementioned abomination, for either discussion sake, or
> pointing and laughing, whichever floats your boat:
>
> def foo(bar.is_? Array, !bar.is_empty?, baz.responds_to :webbed_feet)
You can certainly squeeze this into Ruby - Ruby is so flexible and a lot
of people have done all sorts of weird things with the language. I
would not bother a second to seriously consider this...
Kind regards
robert