At 01:49 01/05/2004 +0900, you wrote:
>"Jean-Hugues ROBERT" <jean_hugues_robert / yahoo.com> schrieb im Newsbeitrag
>news:6.0.1.1.0.20040430083010.01ce4ab8 / pop.mail.yahoo.com...
> > Hi,
> >
> > For some class I am redefining operator []=(x). Works great.
> >
> > However when I use it, p xx[] = zz prints zz, not the value
> > returned by my method. It makes some sense yet I would have
> > thought that the responsibility for that should belong to
> > my method. Am I missing something ?
>
>Yes, it's defined that way (see below).

Humm, things are the way they are. When I get annoyed by my
kinds questions I end up answering "that's because it's been
that way for billion years". I am there quoting the E.T. answer
in movie Contact by Robert Zemeckis.


> > class Test
> >   def []=(x)
> >     nil
> >   end
> > end
> > p Test.new[] = "hello" # => "hello", I was expecting nil
>
>Normally you want
>
>def []=(idx, val)
>   nil
>end
>
>And that will always print val regardless of the return value to enforce
>coherence with the simple "x = foo" which always evaluates to foo.

"normally" is relative. Proc class defines some def [*args] call( *args) end
and this is rather convenient.

Coherence is a nice thing.

However, it this case it makes it *IMPOSSIBLE* to implement "by ref" versus
"by value" semantic in Ruby (this is probably *one* of the reasons, not the
only one). I hate to state that something is impossible in Ruby. Additionnaly
"Impossible is not French" quoting Napoleon. Let's say it is "difficult" and
let's hope it not an euphemism.

I am currently implementing a LogicVariable class. Ideally a LogicVariable
is exactly like a variable, wherever you can use a variable, you should
be able to use a LogicVariable.

This is similar to remote objects (on some other plane),
wherever you can use an object, you can use a remote object.
The remote object is a "proxy" for the actual
object. In this example, adding a level of indirection solve the issue
of transparent distribution.

So I want to do something similar with variables (which in Ruby today are
not Objects, whoever said "everything in Ruby is an Object" was almost right)

A LogicVariable is a "proxy" to a regular variable. The difference is
related to the fact that a LogicVariable is either "bound" or "free",
meaning "it has a value" or "it has no value". In Ruby all variables
are "bound", even if to nil (most variables actually don't exist at all,
as defined? can tell, this is another path I may want to explore).

LogicVariable is just an example of something impossible to do. Basic
References are impossible to do too. There is no way you can *transparently*
use a Reference to a variable instead of the variable's content itself.

This may be an academic discussion and one may think that I should
use a language with By Reference semantic if I really need it. OTOH,
if there is value in By Reference, it may prove interesting to introduce
that well known concept in Ruby.

How to do that is yet to be determined. But as I progress in prototyping
it, I am collecting requirements. That []= would control the expression
value is probably one of then. Once I am done, I will summarize my
discoveries and (if it makes sense) I will issue a RCR.

Thanks for your help.

Yours,

Jean-Hugues


>irb(main):007:0> class Foo
>irb(main):008:1>   def []=(idx, val)
>irb(main):009:2>     p [idx, val]
>irb(main):010:2>     nil
>irb(main):011:2>   end
>irb(main):012:1> end
>=> nil
>irb(main):013:0> Foo.new["a"]="b"
>["a", "b"]
>=> "b"
>
>Regards
>
>     robert

-------------------------------------------------------------------------
Web:  http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17