From: Sean O'Dell Date: 2004-06-08T08:31:15+09:00 Subject: Re: How to ducktype a Hash? On Monday 07 June 2004 15:42, Gavin Sinclair wrote: > On Tuesday, June 8, 2004, 2:50:50 AM, Sean wrote: > > On Monday 07 June 2004 07:35, Austin Ziegler wrote: > >> Actually, Ruby has both types and classes. Many developers confuse > >> types and classes because in C++ they are one and the same; in Java > >> they are slightly different, but not noticeably so (with the > >> introduction of interfaces). > > > > Ruby has no types. All of Ruby's classes are described, and objects > > instantiated, with an arbitrary set of methods and purpose. There is > > nothing anywhere in Ruby to reflect on the purpose of a class. > > So are there types in Java, such that would fit the above description? > What about C++ (no interfaces)? Other languages? I like interfaces better than classes, but C++ classes serve pretty well for typing. Anywhere you have an object (int, String, whatever) that can be described as adhering to some principal structure and purpose, that's a type I think. Ruby, with some effort, can have nearly all the same typing features as C++...but the problem is, you have to hack at it. The real advantage of typing isn't just that you can do it when you need to, but when it's done for you easily. You can rip right out any advantages to typing by simply making it too hard to use. > FWIW, I think the concept of types in Ruby is wishy-washy. The sort > of thing that humans can talk about but not computers. But the > concept is still there. And ultimately, it's not that different in, > say, Java, where (as you have pointed out) you can abuse an interface. > > So how can you reflect on the purpose of a class in Java? Or is it > all a matter of degree? I think the enforcement mechanisms of inheritance leads C++ and Java developers to more carefully craft out their classes, and that generally means that if you need an object with certain capabilities, you simply derive from a base class and ask for an object of the base class type where you need it. That, I think, was what the C++ and Java language designers had in mind for inheritance. It was a way to both describe the purpose of an interface, and provide implementations of the interface. That said, I don't like the inheritance way of doing things. I did that a lot in C++, and Ruby taught me it wasn't necessary and now looking back, I remember a lot of frustration with it. In fact, I used to LOVE Object Pascal because it had a mechanism where you could plug in your own methods to replace class methods. Object Pascal's version of function pointers really helped me get away from the enslavement of monolithic class hierarchies. So I'm at a middle ground now. All I want is Ruby the way it is, with the ability to tell me if a given object adheres to a certain interface or not. Sean O'Dell