Re: Question about XThis
"David A. Lee" <[email protected]> Sun, 22 Nov 2009 09:06:21 -0500
| Newsgroups | gmane.comp.java.beanshell.user |
|---|---|
| Message-ID | <[email protected]> |
It all depends on what your goals and expectations are .
You can make really nice "classes" out of functions by returning this.
foo() {
int a = 1 ;
int b = 2;
return this ;
}
c = foo();
print(c.a);
1
----
What you were trying to do is entirely different, a function that simply
creates *another object* and returns *that*
Thats entirely different from the "XThis" concept.
You can have it both ways.
If you want to mix the 2 concepts you could do this :
init_txservice(){
example.TransactionService ts =
new example.TransactionService.TransactionService(accountdao.initialize());
return this;
}
myservice = init_txservice();
myservice.ts.doSomething()
David A. Lee
[email protected]
http://www.calldei.com
http://www.xmlsh.org
812-482-5224
neo anderson wrote:
> Yes. That's the thing that goes weird.
>
> Now I've changed every return instance to its own variable, instead of this.
> For instance,
>
> init_txservice(){ ... return ts; } // return instance of ts
>
> not
>
> init_txservice(){ ... return this; }
>
> The problem now goes away. But what I am curious is that previously each
> code block is
>
> init_xxxxxx(){ ... return this; }
>
> They all return `this'. It does compile and no error thrown out until I add
> the code block - init_txservice() { ... }
>
> If return this is wrong. Why other code block get compiled? If it is not
> worng, why init_txservice() { ... } throws error?
>
> Thanks again for your help.
>
> I appreciate it.
>
> Christophe Vanfleteren wrote:
>
>> Shouldn't you return ts instead of this? ie:
>>
>> init_txservice(){
>>
>> import example.TransactionService;
>>
>> ts = new TransactionService(accountdao.initialize());
>>
>> return ts;
>> }
>>
>> txservice = init_txservice();
>>
>> On Sat, Nov 14, 2009 at 8:13 PM, neo anderson
>> <javadeveloper999-/[email protected]> wrote:
>>
>>> I encounter a problem when obtaining Java object from beanshell script.
>>> The
>>> error shows
>>>
>>> java.lang.ClassCastException: bsh.XThis cannot be cast to
>>> example.TransactionService
>>>
>>> code snippet used to create that instance is
>>>
>>> init_txservice(){
>>>
>>> import example.TransactionService;
>>>
>>> ts = new TransactionService(accountdao.initialize());
>>>
>>> return this;
>>> }
>>>
>>> txservice = init_txservice();
>>>
>>> After re-reading the bsh docs, in `Scripted Objects '
>>> (http://www.beanshell.org/manual/bshmanual.html ) it says that this can
>>> be
>>> used to create scripted object using method closures. So it looks like my
>>> code to create txservice is not correct; And when I switch to return ts (
>>> ts
>>> = new TransactionServcie(...) ) the error seems to go away.
>>>
>>> But now a question raises. I use `return this' in other method closure
>>> which
>>> has similar way to return instance, but why other code snippet does not
>>> throw the same error i.e. ClassCastException?
>>>
>>> Other code like
>>>
>>> init_mock_datasource(){
>>>
>>> import org.clin4j.framework.domain.mock.Driver;
>>>
>>> d = "com.mockdb.jdbc.Driver";
>>> url = "jdbc:mockdb://localhost/ecommerce";
>>> id = "user";
>>> pass = "password";
>>>
>>> driver = new Driver(d, url, id, pass);
>>>
>>> return this;
>>> }
>>>
>>>
>>> when objecting java object using this code snippet, everything works
>>> well.
>>>
>>> Does any reason can explain this problem?
>>>
>>> Thanks for help.
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Question-about-XThis-tp26352954p26352954.html
>>> Sent from the BeanShell - User mailing list archive at Nabble.com.
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
>>> 30-Day
>>> trial. Simplify your report design, integration and deployment - and
>>> focus on
>>> what you do best, core application coding. Discover what's new with
>>> Crystal Reports now. http://p.sf.net/sfu/bobj-july
>>> _______________________________________________
>>> Beanshell-users mailing list
>>> Beanshell-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>>>
>>>
>> ------------------------------------------------------------------------------
>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
>> 30-Day
>> trial. Simplify your report design, integration and deployment - and focus
>> on
>> what you do best, core application coding. Discover what's new with
>> Crystal Reports now. http://p.sf.net/sfu/bobj-july
>> _______________________________________________
>> Beanshell-users mailing list
>> Beanshell-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>>
>>
>>
>
>
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july