L
Logan Capaldo
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).