Re: OOP packages



On 5/10/12 8:04 PM, hwfwguy@xxxxxxxxx wrote:

> I may have asked the wrong question. It's hard to evaluate the many
> OOPs out there. Maybe what I'm looking for is a sample problem that
> is solved in several dialects of OOP. That would provide some
> metrics.

It helps to know exactly what you want. Everyone seems to have their own definition of what constitutes a minimum set of behaviors for object programming.

> Having spent the day in C#, the object.method syntax is starting to
> grow on me.

> The primary use of OOP seems to be encapsulation, followed by its
> cousin polymorphism. Rather than adopt an OOP with hundreds of LOC, I
> might adopt one with tens of lines.

~40 lines or less? (compiles to 1652 bytes on Carbon MacForth)

-Doug


\ micro-FMS Douglas B. Hoffman 05/15/12 largely untested

\ Full encapsulation of data and methods.
\ Polymorphism with no restrictions on inheritance order.
\ Dynamic (late) binding of methods.
\ Duck typing.
\ Instantiate objects in the dictionary or the heap.

0 value self
0 value ^class
: dfa 1 cells + ;
: sfa 2 cells + ;
: wida 3 cells + ;
4 cells constant classSize
: find-method ( sel class -- xt)
begin @ dup while 2dup cell+ @ = if 2 cells + nip @ exit then
repeat throw ;
: execute-method ( o xt --) self >r swap to self execute r> to self ;
create object classSize here over allot swap erase
: set-search-order ^class >r get-order begin r@ wida @ swap 1+
r> sfa @ >r r@ object = until r> drop set-order ;
: :class ( "name" --) create ;
: <super ( -- wn..w1 n)
here to ^class classSize allot ' >body dup ^class classSize move
^class sfa ! get-order wordlist ^class wida ! set-search-order ;
: ;class ( wn..w1 n --) set-order ;
: send ( o sel --) over 1 cells - @ find-method execute-method ;
: selector ( "name" --) create does> send ;
: getselect ( -- sel) >in @ bl word find
if >body nip else drop >in ! selector here then ;
: :m ( "name" -- a xt) forth-wordlist set-current
getselect align ^class here over @ , swap ! , here 0 , :noname ;
: ;m ( a xt --) postpone ; swap ! ; immediate
: super ( "name" --) ' >body ^class sfa @ find-method compile, ; immediate
: (ivar) ( "name" -- a) create immediate ,
does> @ postpone literal postpone self postpone + ;
: bytes ( n "name" --) align ^class wida @ set-current
^class dfa @ (ivar) ^class dfa +! ;
: pre-obj ( ^class -- n class n+cell) dup dfa @ swap over cell+ ;
: post-obj ( n class o -- o) dup cell+ >r ! r@ swap erase r> ;
defer allotocate ( n+cell -- o)
: makeobj ( class -- o) pre-obj allotocate post-obj ;
: dict-allot ( n+cell -- o) align here swap ( n ) allot ;
: heap-allocate ( n+cell -- o) allocate throw ;
: make ( xt -- o) is allotocate ' >body state @
if postpone literal postpone makeobj else makeobj then ;
: dict> ( class --o) ['] dict-allot make ; immediate
: heap> ( class --o) ['] heap-allocate make ; immediate
: <free ( o --) 1 cells - free throw ;

\ examples

:class var <super object
1 cells bytes data
:m !: ( n -- ) data ! ;m
:m @: ( -- n ) data @ ;m
:m p: self @: . ;m \ print self
:m init: 5 self !: ;m
;class

dict> var value x
x init: ok
x p: 5 ok

heap> var value h
55 h !: ok
h p: 55 ok
h <free ok

:class var2 <super var
1 cells bytes data2
1 cells bytes data3
:m dump: data dup . @ . data2 dup . @ . data3 dup . @ p: ;m
:m init: super init: dict> var data3 ! ;m \ embed object in ivar
:m test: data3 @ p: ;m
;class

dict> var2 value v2

v2 init: ok

v2 dump:
16864328 5 16864332 0 16864336 0 ok

v2 test: 0 ok

.



Relevant Pages

  • Re: Object Oriented PHP vs Java
    ... Not to beat a dead horse but WHAT IS OOP? ... You can design and program OOP with just about any language - even BASIC. ... var m_dangerousToUse ... This works fine if you trust your programmers and they are disciplined enough to only use what they are "allowed" to use. ...
    (comp.lang.php)
  • Re: OO javascript... this doesnt appear to point to the object
    ... matter, they are the same. ... too ambiguous way (say OOP or not OOP, ... var f = function; ... - shows the stack state for function something ...
    (comp.lang.javascript)
  • Re: still not working: why?
    ... Can someone fix it so i can "study" the working one? ... var $uno=10; ... While not particularly useful in this instance, it would be a more accurate example of OOP. ...
    (comp.lang.php)
  • Re: still not working: why?
    ... var $uno=10; ... OOP is a very different way of thinking, but a much superior way ... of programming. ...
    (comp.lang.php)