From: dblack@... Date: 2002-08-11T22:25:43+09:00 Subject: Re: ambiguity between local variable assignment and writter method Hi -- On Sun, 11 Aug 2002, George Ogata wrote: > Tom Sawyer wrote: > [George wrote:] > >> I'd personally rather lose the o.member = 'something' notation for method > >> calling. As David Alan Black mentioned: > >> > >> >> Isn't it good to have to know when you're calling a method and when > >> >> you're assigning to a local variable :-) I didn't mean that as an anti o.member = 'x' argument though :-) It was in response to Tom's having listed needing to know when to write "self." among the cons of the variable/method ambiguity. My point was just that one already knows one is calling a method; there's no extra knowledge involved. > > so you think writer methods are the problem? what would they be replaced > > with? > > set_x methods, as is used in most traditional OO languages. They should > still be magically generated using attr_*. > > I understand the motive of member=(), though. Since the interface of a > class consists only of methods, o.member = 'x' has no meaning, so why not > make that a method too? The problem is when it's used inside a class, when > you can omit the "o.", and a gotcha arises from potential ambiguity. I > wonder if simply leaving "o.member = 'x'" as illegal ruby would've been > better. I would describe it in a different sequence: it's used inside a class, you *can't* omit the 'o', you write the 'o', there's no ambiguity :-) I think one thing that needs to be taken into account is how little impact this actually has on code -- in comparison to the impact there would be, visually, if every local variable assignment looked different, or if every "obj.thing = x" became something else. Here are some rough statistics. The output of /tmp/lines is the content of all *.rb files in and below ruby/lib, filtering out "def meth..." lines and comment lines (including block comments). Local variable assignments: $ /tmp/lines | grep "\(^\|[[:blank:]]\)[a-z_]\w\+ *=[^=>~]" | wc 1863 7471 58965 Calls to '=' methods: $ /tmp/lines | grep "\b\w\+\.\w\+ *=[^=>~]" | wc 195 667 6360 Subset of above, where receiver is explicit "self": $ /tmp/lines | grep "\bself.\w\+ *=[^=>~]" |wc 34 119 1109 Of course the ratio will vary from one case to another (and I don't guarantee the perfection of the filtering or counting :-), but this suggests strongly to me that having to name the receiver under some circumstances is a small price to pay for the overall cleanness of assignment code. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav