Trans
1/28/2005 2:04:00 AM
Curt Sampson wrote:
> On Thu, 27 Jan 2005, Trans wrote:
>
>
> Indeed. I'm coming more and more over toward the functional side.
Me too.
> > Hm... but doesn;t that still leave it all up to the programmer?
>
> Sure, but it's not hard if that's how the programming culture works.
And
> the only option beyond that is to make ruby purely functional, so
that
> it's impossible to have side-effects. Not even Scheme went that far.
But maybe it _is_ a good idea to go that far.
> > And it is terribly inefficient.
>
> Again, I don't buy this. Show me some proof.
Well, I shouldn't say terribly I guess. But it will be slower by the
simple fact that when passing by-value, a copy of the object must be
made. That operation, though very quick, will add up. I could do a
benchmark...maybe when I have more time.
> I understand that, but I don't like it because it's no longer simple.
> For example, when I read "a.foo(b, c)", which argument allows
> side-effects? Or is it neither? You have to go track down the
definition
> of foo to see what it returns, since there's no indication in that
bit
> of code.
Try this on for size. Calling 'a.foo(b,c)' flags the parameters b and c
as by-value, but nothing actually happens yet. Only if during the
course of executing foo should b (or c) be _affected_ does a copy get
made prior to the actual affect, and the copy is used from then on.
Sort of a lazy pass by-value. (Maybe this is how seme systems already
work?)
Now take a different example 'b = a.foo(b,c)'. Here b will be flaged as
_potentially_ by-reference. Now lets say foo is defined like:
| def foo(x,y)
| x << y
| x
| end
In this case b is simply returning to wence it came. If it were
possbile for the evaluator to see that fact, then the lazy by-value
copying could be omitted. Of course the trick is seeing that (is it
possible?).
It might help to look at a much simpler example:
x += y
Presently, of course, this creates a new object referenced by x. Yet
does it need to? Well, only if there are other references to x's
original object. If there are not then it may just as well be modified
in place, it won't effect anything.
Seems to me if this could be done then it would be by-value with much
of the efficency of by-reference; and also out of sight, out of mind to
the end-programmer. At least, thats the idea. I may have totally
overlooked something, and I don't now how feasibile it is either, or if
the loss in efficency from actually doing it counteracts the gain. But
it's an interesting notion at least.
T.