in your list, you are making 5 copies of the same object. (return value of 5 
copies of the same locale) 

this should be what you want.

list =:  conew &> 5$ <'GameObject'  NB. should return 5 distinct locales

inl_z_ =: (cocurrent@] ".@] [)"1 0

    ] list =. cocreate &> 5$ a: 
┌─┬─┬─┬─┬─┐ 
│5│6│7│8│9│ 
└─┴─┴─┴─┴─┘ 
   'px =: ' (, ":)"1 0  i.5 
px =: 0 
px =: 1 
px =: 2 
px =: 3 
px =: 4 

   ('px =: ' (, ":)"1 0  i.5) inl"1 0 list 
0 1 2 3 4 
   'px' inl list 
0 1 2 3 4

   'px' inl {: list 
4 
   px__go [go =. {: list
4 

----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc: 
Sent: Tuesday, June 3, 2014 8:42:36 AM
Subject: [Jprogramming] Strange Object Behaviour in J

I'm currently experimenting with OOP J. Creating objects and playing with 
them.This is my code:
NB. My class def
coclass 'GameObject'      px =: 0   py =: 0   pz =: 0      movex =: 3 : 'px =: 
y'      destroy =: codestroy
NB. lets make instances and play.
cocurrent 'base'     go =: conew 'GameObject'
px__go
0 NB. returned 0, that is what I expected!
NB. move the px value...
movex__go 5
5   px__go
5 NB. again this is what I expect.
NB. create a list of GameObjects...
list =: 5 $ conew 'GameObject'  NB. I don't understand the output here. list
┌─┬─┬─┬─┬─┐│2│2│2│2│2│└─┴─┴─┴─┴─┘
NB. anyway let's at least try to get a hold of one of the objects.
go =. 0{list   px__go
0 NB. Wow, it worked!
   movex__go 5
5   px__go
5   go2 =: 1{list   px__go2
5

The very end, "px__go2" is troubling, because this object's px value is 5! The 
only way this can be is if(a) 1{list and 0{list somehow point to the same 
object,(b) px is a static variable.
I'm going to guess (a), but I would like to know what is going on here.

Secondly, in my object definition, I would like to have a "create" verb which 
does the following:

create_go  1 4 5

this will set px to 1, py to 4 and pz to 5. I'm not sure how to do multiple 
assignments in J with on function.
I think if it wasn't a class function I could do (for three arbitrary variables)


'px py pz' =: 3 NB. sets all vars to 3, not really what I want, but it's a 
start.

or something like that, but for class functions I don't know how to do this. 
Any help appreciated.
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

Reply via email to