From: Ryan Pavlik Date: 2003-11-05T18:08:04+09:00 Subject: Re: Managing metadata about attribute types On Wed, 5 Nov 2003 16:13:37 +0900 Austin Ziegler wrote: > It isn't inferior, and you don't need type info. Remember -- an > object should validate or transform its own data. I'm trying to stay out of this because I mostly disagree. This however warrants addressing. This is demonstrably wrong. An object cannot validate and transform its own data in this context in any reasonably general manner. It's simple when you're addressing a few basic types... String, Float, Integer, Hash and Array. This isn't general, though. What if I want a Foo, and you give me a Bar? Foo was from one module (which shouldn't know about Bar), and Bar was from another module (which shouldn't know about Foo). There is there no #to_foo (which may be fortunate depending on your culinary preferences). This leaves us with only a few options: * We just don't allow it. This does no good for us. * We convert through an intermediary type. This is inefficient and may lose data or not work, either. * We decide the approach is wrong and do something else. Using #to_* methods are the ruby equivalent of type casting. The point in this case is not to _convert_ types, it's to provide the right type in the first place. Instead of giving the attribute a string and expecting it to be parsed, we want to create the desired type and hand that off. It has nothing to do with the #attr= function. Strict type checking at that point is merely a convenience. It's all about getting the input into a useful format without writing n^2 functions (for n classes). This is the primary reason I wrote StrongTyping in fact; the strict checking has merely helped with debugging a whole lot. -- Ryan Pavlik "Do not question wizards, for they are quick to turn you into a toad." - 8BT