Yuvaraj Athur Raghuvir wrote:
> To get started, I want a verb that takes a string and creates a
> class and an instance out of it.
> Example:
> c =: create 'customer'
> Must result in a class definition like so :
> coclass 'customer' NB. Gets executed
> create=: cocreate NB. Gets executed
> destroy =: codestroy NB. Gets executed
> followed by creation of an instance that is returned.
> o=. conew 'customer' NB. Gets executed
Ok, that's easy enough:
create=:3 :0
coclass y
create=: cocreate
destroy=: codestroy
o=. conew y
)
Mind you, I think it's bad style to have the word "create" mean two
totally different things. Having two conflicting definitions for the
same word tends to make talking about that word complicated. But with
the above,
c=: create 'customer'
1. creates the class customer
2. makes 'create' and 'destroy' as methods of that class
3. creates an instance of that class which is referenced by c.
Note that o=. doesn't actually do anything useful here, but it's
harmless and I'll let you take it out.
Note also that you can use:
names_customer_''
and
names__c ''
to inspect the contents of the class and the object. (And note that
we have two slightly different formalisms, to distinguish between a
literal reference and an indirect reference.)
> From this point on, I want to add attributes dyamically to
> the class and the instance.
> c add ('name' ; 'MyName') NB. Format <Name,Value> pair
> Internally the add verb does the following:
> a) injects name as an attribute in the class 'customer'
> b) instantiate that value in the instance 'c' that I have.
Ok... the only problem here is that we need to get the name of the
class that object c is an instance of.
Personally, I'd rather the right argument to add be the class name
itself, rather than an object reference. After all, you're adding
this content to the class -- the object inherits from the class, so
gets the updates automatically, but you're not adding the content to
the object. Not directly, at least.
First, a verb to get the class of an object:
instanceOf=: [EMAIL PROTECTED]
instanceOf c
+--------+
|customer|
+--------+
Second, 'add', using my modified specification:
add=:4 :0
((0{::y),'__x')=: 1{::y
)
(instanceOf c) add 'name'; 'MyName'
MyName
name__c
MyName
Note here that name does not yet exist in the object. It's been defined
in the class, and is inherited by the object. The object could have its
own instance of this variable but since it does not have an instance
variable named 'name', it inherits the definition from the class.
If you really prefer your specification, here's how to make that work:
add=:4 :0
((instanceOf 0{::y),'__x')=: 1{::y
)
Or, more directly
add=:4 :0
(({.copath 0{::y),'__x')=: 1{::y
)
> Further, I am unsure on how to execute the blocks created. (I have
> read it somewhere....just that I am grappling with the concept and
> the implementation simultaneously and so thought I would ask the
> forum members for a fast response)
I don't understand what you want to do here.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm