gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gforth] Objects.fs questions!


From: Philip Smith
Subject: [gforth] Objects.fs questions!
Date: Thu, 9 Apr 2015 12:51:45 -0600


This example works but i am not sure if i am doing this correctly:
\ ***************************
require objects.fs
object class
    cell% inst-var a
    m: ( na -- )
        a ! ;m method a!
    m: ( -- )
        ." at atest destruct" cr ;m method destruct
end-class atest

object class
    cell% inst-var holder
    m: ( -- )
        atest heap-new holder ! ;m overrides construct
    m: ( -- )
        cr holder @ .s cr [bind] atest destruct
        ." at ctest destruct" cr ;m method destruct
end-class ctest

ctest heap-new value c.test
c.test destruct cr ." ********"
\ ****************************************

Adding a new class with base clasee ctest and trying to access the destruct method as follows does not work:
\ **********************************************************
require objects.fs
object class
    cell% inst-var a
    m: ( na -- )
        a ! ;m method a!
    m: ( -- )
        ." at atest destruct" cr ;m method destruct
end-class atest

object class
    cell% inst-var holder
    m: ( -- )
        atest heap-new holder ! ;m overrides construct
    m: ( -- )
        cr holder @ .s cr [bind] atest destruct
        ." at ctest destruct" cr ;m method destruct
end-class ctest

ctest heap-new value c.test
c.test destruct cr ." ********"

 ctest class
    cell% inst-var holder2
    m: ( -- )
        this [parent] construct
        atest heap-new holder2 ! ;m overrides construct
    m: ( -- )
        this [parent] destruct
        cr holder2 @ .s cr [bind] atest destruct
        ." at btest destruct" cr ;m overrides destruct
 end-class btest

 btest heap-new value b.test
 b.test destruct cr ." *********"
\ ************************************************************

Now i think i should be able to get this to work if i use interfaces or if i implement the methods later in the code from the class code but i have not done this to be sure if it will work!  
My question is what am i doing wrong in the above code?  I want to basically have a destructor method that could be used to free dynamically created stuff.  But because the name is not unique from class to class i need to bind it to the class the destructor is from.  Is this binding possible inside a class directly or not?  Is it possible to have methods named the same ... it would seem to me that this should be possible because the base class object has construct and print methods and we access them by using "this [parent]" i believe.  
I know i can get around the problem by having unique names for every method but i find objects to be a cleaner way to make self documenting code when i use an object that is.  
Simply is it possible to bind to a method inside a class definition like i am trying above and if not why does it work in the first example?  
I am using Gforth ver 0.7.3 currently !
Thanks as always for the great work i know so many people have done with Gforth!

--
                \|||/
-----0oo---( o o )---oo0------
                (_)
From the hand of PKS


reply via email to

[Prev in Thread] Current Thread [Next in Thread]