Rishi wrote:
> I was really hoping this would work. It seemed like an elegant solution.

>> state: make object! [
[    new: func [
[        puzzle [block!]
[        ][
[        make object! [
[            puz: copy reduce puzzle
[            get-puzzle: :get_puzz
[            ]
[        ]
[    get_puzz: does [return copy puz]
[    ]
>> test-obj: state/new [1 2 3 4 0 5 6 7 8]
>> probe test-obj

make object! [
    puz: [1 2 3 4 0 5 6 7 8]
    get-puzzle: func [][return copy puz]
]
>> test-obj/get-puzzle
** Script Error: puz has no value.
** Where: return copy puz

> I don't understand why get-puzzle cannot access word 'puz.

state/get_puzz is not in the same context as test-obj/puz.

Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
-><-

----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, 18 October 2000 3:04 PM
Subject: [REBOL] objects without overhead Re:(2)


> There were a few mistakes in my previous emails code. I tried out the
basic concept though...but it was a failure. here's a simplified version of
code. This is real code and I have run it through rebol:
>
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CODE BEGINS HERE
> state: make object! [
>   new: func [
>     puzzle [block!]
>   ][
>     return make object! [
>       puz: copy reduce puzzle
>       get-puzzle: :get_puzz
>     ]
>   ]
>   get_puzz: does [return copy puz]
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;: TEST CODE
> ;  comment {
>     test-obj: new [1 2 3 4 0 5 6 7 8]
>     print test-obj/get-puzzle
> ;  }
>
> ]
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CODE ENDS HERE
>
> Now if you copy this code and run it, you get the following error:
>
> ** Script Error: puz has no value.
> ** Where: return copy puz
> >>
>
> I was really hoping this would work. It seemed like an elegant solution. I
don't understand why get-puzzle cannot access word 'puz.
>
>
> Previously, you ([EMAIL PROTECTED]) wrote:
> > Well...I thought about this a bit and I THINK I found an elegant
solution. So I guess I'll respond to my own question...
> >
> > I think the solution to this problem is inner objects. Here is an
example:
> >
> > myobject: make object! [
> > -  function1: func[][..]
> > -  function2: func[][..]
> > -      .
> > -      .
> > -  make: func[var1 var2 var3][
> > -    return make object! [
> > -      instancevar1: var1
> > -      instancevar2: var2
> > -      instancevar3: var3
> > -
> > -      instancefunc1: function1 ;copies a reference to function1
above...
> > -      instancefunc2: function2
> > -    ]
> > -  ]
> > ]
> >
> > ;;;create instance of object
> > coolobj: myobject/make
> > coolobj/instancfunc1   ; all instances access same function! less
overhead if many objects!!!
> > .
> > .
> > .
> > I am not sure if this would work since I have never tested it (yet). The
questions in my mind are:
> > 1. are functions copied by reference or value? if by value, this would
not solve my problem
> > 2. do inner objects have access to outer object words? for example, can
'instancefunc1 access 'function1 as shown above?
> >
> > I hope this works. If it does, it would be a very simple but elegant
solution to my problem. It would be even more elegent when modules come
around so I can keep all private words in context of myobject rather than
the actual object returned...making code very clear.
> >
> > Rishi
> >
> >
> > Previously, you ([EMAIL PROTECTED]) wrote:
> > > I am working on a program which requires about 1000 or so instances of
objects. It doesn't have to be OO but I like the design better that way. I
am using a function as constructor like the following example (not real
code..):
> > >
> > > ;I used dashes to keep formatting intact (hopefully this works)
> > >
> > > make_myobject: func[var1 var2 var3][
> > > --return make object! [
> > > ----instancevar1: var1
> > > ----instancevar2: var2
> > > ----instancevar3: var3
> > >
> > > ----instancefunc1: func[][...]
> > > ----------------.
> > > ----------------.
> > > ----------------.
> > > -]
> > > ]
> > >
> > >
> > > The thing I don't like about this solution is that every instance of
this object has duplicate copies of the functions (As far as I know...please
correct me if I am wrong). This seems like a waste to me - especially if the
object has 50 or so functions. Is there any OO way in Rebol to create
multiple instances of this object without creating multiple copies of the
functions associated with each object? I know how to do this in a non-object
oriented fashion but would like to see an OO solution.
> > >
> > >
> >
> >
>

Reply via email to