The method pop as listed doesn't return the popped item; it returns the
size of the stack after the first element is popped. To have it return
the popped item you can instead write
pop =: 3 : 'top [ (items =: }.items) [ top =. {.items'
.
Marshall
On Sat, May 31, 2014 at 05:32:02PM +0100, Jon Hough wrote:
> I'm trying to get my head round OOP in J.
> Mainly with http://www.jsoftware.com/help/learning/25.htm
>
> My current problem is if I create an instance of a class and, and I want to
> use it as a member variable of another object, how can I do this and then
> call it and its functions?
> e.g.
> Using the Stack example:
> coclass 'Stack' create =: 3 : 'items =: 0 $ 0' push =: 3 : '# items =:
> (< y) , items' top =: 3 : '> {. items' pop =: 3 : '# items =: }.
> items' destroy =: codestroy S =: conew 'Stack' create__S 0
> push__S 2
> 1 NB. create new stack T
> T =: conew 'Stack' create__T 0
> push__S T
> 2
> Now T is on S's stack. And I can retrieve it by popping, but doing this does
> not seem to returna reference to the actual object T, and I cannot call any
> of T's methods. Is there a way to cast the returned item to a stack? Then
> possibly I can call it's methods.
> A Java example (not using generics):
>
> public class MyStack{..// definitions etc}
> MyStack S = new MyStack();
> MyStack T = new MyStack();
> S.push(T);
> MyStack U = (MyStack)S.pop();//cast returned object to a MyStack
> U.push(5);//do something with returned object.
> Regards.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm