In article <2025Oct5.131926@mips.complang.tuwien.ac.at>,   
   Anton Ertl wrote:   
   >Krishna Myneni writes:   
   >>On 10/4/25 11:53 AM, Anton Ertl wrote:   
   >>> One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a   
   >>> handle for the current object.   
   >>   
   >>We needed "this" for use in our code at work. In those instances where   
   >>it was needed, it was simple enough to implement and use within the   
   >>framework of mini-oof.fs in the following manner:   
   >>   
   >> class \ define a class derived from class x   
   >> ... \ specify vars and members   
   >>end-class    
   >>   
   >>For every class member of which needs "this", define the member as   
   >>follows,   
   >>   
   >>0 ptr this \ ptr is synonymous with VALUE in ANS Forth   
   >>:noname ( o -- )   
   >> to this   
   >> ...   
   >> this V1 @ \ V1 is a var which belongs to the class for object o   
   >> ...   
   >> ; \ close the noname definition, leave xt on the stack   
   >> defines    
   >   
   >[I inserted the line that you added in <10bsgj8$2srh2$1@dont-email.me>]   
   >   
   >This code does not save THIS on entering the word and restore it on   
   >exit. Which plays a role if you call another method with a different   
   >object of the same class (e.g., when your object is a tree node, and   
   >you want to do something for both child nodes). And once you save and   
   >restore THIS, a per-class THIS does not buy you anything.   
   >   
   >The saving and restoring of THIS is what M: and ;M do in objects.fs.   
   >So that's the way I prefer to deal with THIS, and it means that I   
   >prefer to pass the object for which a method is to be called on the   
   >data stack.   
   >   
   >By contrast, Bernd Paysan prefers to manipulate the current object   
   >explicitly and to implicitly pass the object for which a method should   
   >be called through the current object, as exemplified in mini-oof2. So   
   >you write code as follows:   
   >   
   > mini-oof2 objects.fs   
   >invoke O.M, with O in THIS already: M O M   
   >invoke O.M, with O not yet in THIS: O >o M o> O M   
   >define a method implementation: :noname ... ; M: ... ;M   
   >access THIS unneeded THIS   
   >   
   >Note that O> is not like R>, but like RDROP.   
   >   
   >Concerning implementation, one (or several) global values THIS only   
   >work in single-threaded programs. For multi-threaded programs, you   
   >need to use a UVALUE (i.e., a thread-local value), or use a different   
   >approach. A simple approach that you can use in mini-oof is to define   
   >THIS as a local; your example above becomes:   
   >   
   >:noname {: par1 par2 this -- ... :}   
   > ...   
   > this v1 @   
   > ...   
   >;   
   > defines    
   >   
   >Another alternative is to have special support for the current object   
   >in the Forth engine, which is what mini-oof2 is doing for >O and O>;   
   >but that means that the Forth engine also needs to know about method   
   >calls and instance variable accesses (they also work with the current   
   >object and access it internally).   
      
   What is the big deal?   
   I have M: .. M; normally working on a current object.   
      
   Max Probst's lisp works with objects 1] in the stack,   
   so I added   
      
   : M:r BLD} HERE DP-MARKER @ - >R SWAP-DP : R> "%d +" FORMAT&EVAL ;   
      
   1] Actually he used structs.   
   >   
   >- anton   
      
   Groetjes Albert   
   --   
   The Chinese government is satisfied with its military superiority over USA.   
   The next 5 year plan has as primary goal to advance life expectancy   
   over 80 years, like Western Europe.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|