Re: [Beanshell-users] Pointers and Values (and mixins)

Pat <[email protected]>
Newsgroups gmane.comp.java.beanshell.devel,gmane.comp.java.beanshell.user
Message-ID <[email protected]>
On Mon, Sep 15, 2003 at 05:36:55PM +0200, Pierre-Mikael Legris wrote:
>         public String str1 = "str1";
>         public String str2 = "str2";
>         public MyString str3 = new MyString("str3");
>         /** Creates new form test */
>         public test() {
>                 i = new Interpreter();
>                 try {
>                         i.set("test",this);
>                         i.set("str2",str2);
>                         i.set("str3",str3);
>                         i.eval("test.str1 = \"str1_modified\";");
>                         i.eval("str2 = \"str2_modified\";");
>                         i.eval("str3.set(\"str3_modified\");");
>                         System.out.println(str1);
>                         System.out.println(str2);
>                         System.out.println(str3);
...
> I expected str2 to be modified..... 

Hi,

Your code above is working as I would expect.  The above situation is 
equivalent to the following plain Java code:

  String str2 = "str2";
  String bshVariableStr2 = str2;
  bshVariableStr2 = "modified";
  System.out.println( str2 ); // "str2";

A normal BeanShell variable is a separate variable.  In this case it's a 
reference to the same string you created for str2.

One solution, as you have demonstrated above, is to refer to str2 through the
'this' reference to your enclosing object.  That's how you'd have to do it in
plain Java code.

In BeanShell you also have the option of using a "mixin" - a new feature in
BeanShell 2.0 - like so:

  i.set("test", this);
  i.eval("importObject(test)");

What this does is to bind all of the variables and methods of the "test" object
into the BeanShell namespace.  In that case, they do indeed act like
references.  So then:

  i.set("str2", "modified");
  System.out.println( str2 ); // "modified"


The user manaul has not yet been updated for BeanShell 2.0, but again, the
original behavior is the same as Java. 


Thanks,
Pat Niemeyer



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.