From: Logan Capaldo Date: 2006-09-09T08:51:52+09:00 Subject: Re: better alias_method On Sep 8, 2006, at 5:58 PM, TRANS wrote: > On 9/8/06, Sylvain Joyeux wrote: >> > a = 1 >> > a = a + 1 >> > >> > def a ; 1 ; end >> > def a ; a + 1 ; end >> > >> > This would eliminate the need for #super except in cases of >> recursion, >> > in which case a special notation is also needed for the >> definition. Eg. >> > def_rec a, or something. But that's cool b/c it would be safer. > >> I don't see how you write "recursive methods which call super". > > Like is Haskell you'd have to explictily state it's recursive. So for > instance I'm suggesting: > > class A > def x; "x"; end > end > > class B < A > def x; x.upcase; end > end > > Ordinarily B#x would recurse (and be an infinite loop). But not so in > my suggestion. So the above would work and rather you'd have to tell > it esspecially if recursion were desired, something like: > I'm pretty sure this would be impossible (also you are thinking of ML, not Haskell (Haskell's defs are recursive by default, it is in ML where you have to say let rec (and Lisp/Scheme as well) to define a recursive function) in ruby since ML and friends find those functions lexically at compile time. I guess the parser could do the equivalent of s/#{current_method}/super/g on the body of the function, but that just makes it alternate syntax and doesn't solve this problem. > class A > def x(n); n - 1 ; end > end > > class B < A > def_rec x(n) > x == 1 ? 1 : n + x(x(n)) # err... x(super(n)) > end > end > > Now in this case how do you call A#x from B#x if you need to? We would > still need to have #super as shown in the comment. > > Hmm... better yet, you could do without super altogther and also not > need the special def_rec if we had a special method that meant "this > method", maybe #this. So: > > class B < A > def x(n) > x == 1 ? 1 : n + this(x(n)) > end > end > Forth has this, it's called "recurse" there. (also Joy has a whole slew of words for recursion with anonymous functions). > Would work for the recursive case. > > T. >