This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Static Duck Typing?
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
That got me thinking about Ruby, and previous conversations regarding optional static typing. Maybe we're approaching this from the wrong end of the stick. Instead of checking type, we should use duck typing, but in a static fashion.
Whoa, stop laughing and hear me out.
Currently in Ruby, a traditional duck typing approach looks like this:
def foo(arg)
raise SomeError unless arg.respond_to?(:bar)
...
end
What if we could marry the concept of static typing with duck typing? Then, the syntax might look like this:
def foo(arg{:bar})
...
end
Then we add a compile time step to Ruby (remember, I'm in fantasy land here), so that if you try to pass any object to the foo method that doesn't respond to bar, it would automatically raise a NoMethodError without you having to add your own respond_to? checks. Perhaps this would have the side effect of improving speed and/or intellisense. Maybe.