From: ahoward Date: 2003-02-01T01:46:25+09:00 Subject: Re: String#+ On Sat, 1 Feb 2003, Chris Pine wrote: > I always assumed there was some good reason for it (even if it was a bit > confusing, and probably the source of fully half of my newbie mistakes, and > which requires a disproportionate amount of my tutorial to explain...). > However, if there is a good reason for it, I still don't see what it is. > (Mind you, I wouldn't be surprised if there was one, but at this point I'd > also be surprised if there wasn't one.) there is a good reason IMHO! here is a real eg. i encountered : in ruby 1.6.8 class CGI class Session def []= key, value ... @db[ key ] = value.to_s ... end end end now, ONLY strings can be stored as keys or values of sessions. no big deal you say? well, say you wanted to write CGI::Session::Pstore, which would allow a session to cache arbitrary objects, not only strings like CGI::Session::FileStore or CGI::Session::MemoryStore. (actual code from ruby lib - which i had to modify) class CGI class Session def []=(key, val) unless @write_lock @write_lock = true end unless @data @data = @dbman.restore end #@data[key] = String(val) # original code @data[key] = val end end now you move on and write CGI::Session::Pstore like http://raa.ruby-lang.org/list.rhtml?name=cgi-sess-pstore but you worry that, in the next release the 'little modification' you made to CGI::Session will adversely affect CGI::Session::FileStore and CGI::Session::MemoryStore. as it stands - 1.8 does _not_ cast it's value to string, and so i will be able to remove my hack into CGI::Session. moral : if you cast arguments inside a method you are preventing subclassing/extension and promoting white-box programming. this holds in all languages, for example, if you wrote a C function which did this int function (x) int x; { unsigned char uc = (unsigned char) x; ... } users of this method would be perplexed as to why the argument went in as an int, and all values > 255 seem to be handled identically?! so you start looking into the library code which breaks the whole 'black-box' idea libraies should manifest, etc., etc, down a road fraught with peril... -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================