Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #134255

Re: Examples for gforth's objects.fs

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: Examples for gforth's objects.fs
Date 2025-10-05 11:19 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2025Oct5.131926@mips.complang.tuwien.ac.at> (permalink)
References <877bxcm4eb.fsf@news.teletyp.ist> <2025Oct3.231505@mips.complang.tuwien.ac.at> <10bqkg4$2bksr$1@dont-email.me> <2025Oct4.185351@mips.complang.tuwien.ac.at> <10bs5l3$2obnp$1@dont-email.me>

Show all headers | View raw


Krishna Myneni <krishna.myneni@ccreweb.org> 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:
>
><x> class   \ define a class derived from class x
>   ... \ specify vars and members
>end-class <y>
>
>For every class member of <y> 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
><y> defines <member-name>

[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 @
  ...
;
<y> defines <method-selector>

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).

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html
EuroForth 2025 registration: https://euro.theforth.net/

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Examples for gforth's objects.fs okflo@teletyp.ist - 2025-10-03 16:44 +0200
  Re: Examples for gforth's objects.fs Hans Bezemer <the.beez.speaks@gmail.com> - 2025-10-03 17:11 +0200
  Re: Examples for gforth's objects.fs anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-03 21:15 +0000
    Re: Examples for gforth's objects.fs Gerry Jackson <do-not-use@swldwa.uk> - 2025-10-04 09:05 +0100
      Re: Examples for gforth's objects.fs anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-04 16:53 +0000
        Re: Examples for gforth's objects.fs Krishna Myneni <krishna.myneni@ccreweb.org> - 2025-10-04 17:04 -0500
          Re: Examples for gforth's objects.fs Krishna Myneni <krishna.myneni@ccreweb.org> - 2025-10-04 20:11 -0500
          Re: Examples for gforth's objects.fs anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-05 11:19 +0000
            Re: Examples for gforth's objects.fs albert@spenarnc.xs4all.nl - 2025-10-06 10:46 +0200
        Re: Examples for gforth's objects.fs Gerry Jackson <do-not-use@swldwa.uk> - 2025-10-05 11:54 +0100
          Re: Examples for gforth's objects.fs anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-05 11:56 +0000
          Re: Examples for gforth's objects.fs Hans Bezemer <the.beez.speaks@gmail.com> - 2025-10-05 14:31 +0200
  Re: Examples for gforth's objects.fs Krishna Myneni <krishna.myneni@ccreweb.org> - 2025-10-03 20:21 -0500
  Re: Examples for gforth's objects.fs albert@spenarnc.xs4all.nl - 2025-10-04 15:07 +0200

csiph-web