Placing this in a file and then loading

coclass 'myclass'
a =: 2 NB. member variable inside class or object.  Noun intended.
A =: 1 : ' a=: u a' NB. adverb that will modify member variable

in console (base locale) you can do:

 a =. conew 'myclass' NB. a is variable in base locale
 +: A__a 
4 
 3&+ A__a 
7 
 a__a7
 +: A_myclass_ NB. uses class variable instead of instance variable. future 
conew's would use 4 as the initial instance val
4

 In terms of what you were trying to do, =. is always local to the function =: 
keeps variables within a locale.  I'm not completely sure why top level 
(console) allows =., but I suspect it is just an added debugging convenience.

The intended benefit of the adverb pattern is allowing your code to receive 
variable verbs/functions to apply to the current state of your object, and 
likely to modify its state. Amending an internal table for example.

But another example would be moving the x/y coordinates of a game/visual object

 2 3"_ A__a 
2 3


 2 3&+ A__a [ 2 3"_ A__a 
4 6 


I'm passing a move order that is relative to the existing state.  While there 
is always the alternative of this longer expression:

a__a =: 2 3 + a__a

the big advantage of the adverb approach is that you can extend the adverb to 
not just make the move, but also check for collisions, send a rendering 
command, or perhaps generate a more complex timelapse of the movement from the 
old and destination states, and those features can be added in 2.0/3.0 
iterations without affecting all of the "client"/consumer code that just cares 
about moving tiles around. 



----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc: 
Sent: Tuesday, December 9, 2014 11:15 AM
Subject: Re: [Jprogramming] A neat OOP trick with adverbs




I was playing with your adverbs.Any idea why the following doesn't work?
coclass 'myclass'empty =: ''classname =: myclass
A_myclass_=:    1 :'a =. empty&u classname' 



   conew A


error is:


I get the following error:
|value error: create__w


|       create__w x


My intention was to instantiate 'a' as an instance of myclass (but also keeping 
it private for whatever reason). 









> Date: Tue, 9 Dec 2014 03:31:23 +0000
> From: [email protected]
> To: [email protected]
> Subject: [Jprogramming] A neat OOP trick with adverbs
> 
> adverbs can return nouns, though most are designed to return a verb.
> 
> 1 : 'm + y'  NB. a verb because it accesses y
> 1 : 'u 2'  NB. returns a noun because it doesn't access y even though it uses 
> a verb as adverb parameter
> 
> Consider this noun producing adverb,
> A_z_ =: 1 : 'a =: u a'
> 
> its defined in z, just for convenience.  It applies its verb parameter to a 
> and stores the result in a.  The value of a it will use is whatever locale it 
> is qualified with.
> 
>   a=.1 
>   a_b_ =: 3 
> 
>   +: A 
> 2 
> 
>  +: A_b_ 
> 6 
>  a_b_ 
> 6 
> 
>  a&+ A_b_ NB. the a parameter is taken from caller's locale as u is parsed 
> before it is passed to A_b_
> 8 
> 
> 
>  a_b_ =: i.5 
>  a&(0}) A_b_ NB. sending parameters to amend in place
> 2 1 2 3 4 
> 
> 
> OOP approach often involves creating side effects in class instances.  Noun 
> returning adverbs can access all instance variables (or other program data), 
> and make any side effects it wants.  For instance, it can save or backup data 
> to disk in the last amend example.
> 
> The neat part is that with class defined adverbs, you can send verb messages 
> to your objects.  Even though they are verbs, they can be bound with many 
> caller supplied nouns (such as the 2 parameters to }), and in a tacit 
> expression caller variables get fixed into the supplied verb.  Its also neat, 
> because many other uses of adverbs in objects are unintuitively problematic 
> in their parsing.
> ----------------------------------------------------------------------
> 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

Reply via email to