Thanks for you solution. I don't think it will work in my case though. As to your question, how did I end up not knowing what types I have, I have a bunch of classes, with lots of inheritance, and I want to be able to serialize any instances of these, which means serializing any references, too.
The strategy is, more or less the same as the wiki link I gave previously. First, use nl__myClass '' to find all members of the instance. loop through each member. if it is primitive, serialization is simple, if not then I need to call the serializer on the reference. My method works, and can serialize a class instance, and all its references, into string and output as a file, except in a couple of edge cases, i.e. the problem in my original email. Thanks, Jon -------------------------------------------- On Tue, 7/10/18, Raul Miller <[email protected]> wrote: Subject: Re: [Jprogramming] Test whether class member is primitive To: "Programming forum" <[email protected]> Date: Tuesday, July 10, 2018, 11:49 PM The trick here, of course, is that objects are not values - you can only have a reference to an object as a value, and that reference is itself a primitive member of the referring object. Which brings up the issue: how did you lose track of what it was you were dealing with? If you're building dev tools that's fine. If you're wanting this for production code, though, you might want to rethink your approach. Anyways, ... here's how I'd approach this issue: isRef=:3 :0"0 try. 0 <: 4!:0 y catch. 0 end. ) Thanks, -- Raul On Mon, Jul 9, 2018 at 9:41 PM 'Jon Hough' via Programming <[email protected]> wrote: > > I want to test if a given member variable of an object is primitive (e.g. one of the J datatypes - literal, integer etc etc) or an object. > > coclass 'MyClass' > > create=: 3 : 0 > m=: 'something' > ) > > > Here is one method: > > myClass =: '' conew 'MyClass' > a: -: {. (copath ::( a:"_) m__myClass) -. <, 'z' NB. return 1 if primitive, 0 if an object. > > This works, except if the member is defined in a parent class. > > coclass 'MyOtherClass' > coinsert 'MyClass' > > create=: 3 : 0 > create_MyClass_ f. '' > '' > ) > > > myOtherClass =: '' conew 'MyOtherClass' > a: -: {. (copath ::( a:"_) m__myOtherClass) -. <, 'z' > > > the above returns 0, but member is a primitive member of the MyOtherClass instance. Ideally this > should return 1 in this case, since m is still a primitive member of MyOtherClass. > > Any better way to test for primitive members? > > Thanks, > Jon > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
