From: Charles Hixson Date: 2003-11-23T00:01:24+09:00 Subject: Re: "stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types) ) Austin Ziegler wrote: >On Fri, 21 Nov 2003 02:20:04 +0900, Sean O'Dell wrote: > > >>On Wednesday 19 November 2003 10:47 pm, Austin Ziegler wrote: >> >> >>>On Thu, 20 Nov 2003 14:52:17 +0900, Thien Vuong wrote: >>> >>> >>>>I'm in the same position too. Love Ruby but cannot recommend it >>>>for team development w/o having the language provides some sort >>>>... >>>> >... > > int max(int a, int b) { return a > b ? a : b; } > float max(float a, float b) { return a > b ? a : b; } > double max(double a, double b) { return a > b ? a : b; } > char max(char a, char b) { return a > b ? a : b; } > String max(String a, String b) { return a > b ? a : b; } > Complex max(Complex a, Complex b) { return a > b ? a : b; } > >or: > > def max(a, b); a > b ? a : b; end > >This is duck typing at its best: as long as a can be compared >against b (either by supporting #> directly or through #<=>), then >max will work for the specified classes. We don't even have to be >comparing two of the same types. >... > >-austin >-- > > > but do also consider: max(5.3, "austin") Which is silly if you are using literals, but which could be a = 5.3 ... b = "5.25" ... max(a, b) Using duck typing, of course, doesn't free you from the requirement of error checks (I almost said type checks here :-) One *does* need to ensure that the formats are compatible. Even numerics aren't sufficient (consider kilometers vs. miles, e.g.). But without context one doesn't know whether max should do the automatic conversion, or raise and exception. And context is the real problem here. Type systems are attempts to capture context. One can reasonably argue that they are better than nothing. Classes are other attempts to capture context. Neither is, or should be made, perfect. But how context should be specified is far from clear. Even when assertions are built into the system, as they are with Eiffel, they are far from perfect. In Ruby... well, they exist, but I don't know how to use them, and assert doesn't show up in the pickaxe book (at least a grep of the online version didn't find it). *I* sure don't know what the right answer is. The flexibility that makes Ruby (and Python) so powerful is not something that should be readily given up. (And the benefits of doing so are, in any case, problematic.) But I do frequently want a convenient way for my routines to be able to check on their parameters, and how they can be used. Unfortunately, there doesn't seem to be a decent way to specify unit types (outside of creating separate classes for the different units). Fortunately that's not impossible, and with open classes not even infeasible. But it sure is a lot more work than just typing 3 ft. 7 in.