Problems creating class of mutable arrayBuffers objects

If you have any questions, remarks, ... if you need help... its here...

Problems creating class of mutable arrayBuffers objects

Postby bobgillies » 16 Feb 2017 14:14

I'm trying to do this but I'm I can only explain this in ANSI Forth:
I created a word an allot 10 cells to it.
create foo 10 allot

I can store a number to the 0 indexed cell in the >body:
10 foo !

I can fetch a number from the 0 indexed cell in the >body:
foo @

Now I'm trying to create an object in oforth like that for mutable arrays.

Object Class new: foo( n mutable values)

foo method: initialize( n -- )
n := n
ArrayBuffer newwith( n , 0 ) := values
;

foo method: at( n -- x ) \ u x -- item : Returns item at index i, null if none.
at( )
;

foo method: put( n x -- ) \ i x aArrayBuffer -- : Put x at index i of aArrayBuffer
put( )
;

Obviously the method definitions I'm written are left incomplete as I've racked by brain trying to index into the object's data field but I keep coming up with errors due to my lack of comprehension. I'm trying to stay away from relying on task variables as advised in the Manual.

How do write the code to fetch and store into an ArrayBuffer's data fields?
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: Problems creating class of mutable arrayBuffers objects

Postby Franck » 16 Feb 2017 19:52

Sorry but I don't understand why the built-in ArrayBuffer class does not work with what you want to do.

Just create it like this :
Code: Select all
10 0 ArrayBuffer newWith


and you have an mutable array. You can access objects using #at and #put (1-based).

I don't understand what provide creating a new class instead of using the built-in ArrayBuffer class.

Franck
Franck
 
Posts: 144
Joined: 29 Oct 2014 19:01

Re: Problems creating class of mutable arrayBuffers objects

Postby bobgillies » 16 Feb 2017 20:14

I'll try doing things first from the command line. I think I'm coding my source with too many complications from the outset and it's best that I start out with some simple command line examples like you've provided and work from there. Baby steps as they say. :cry:
I'll report back when I hit a snag in my code. Thank you. :)

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: Problems creating class of mutable arrayBuffers objects

Postby bobgillies » 16 Feb 2017 20:32

Franck, #at and #put work from the command line. I seem to be doing something wrong when I'm creating a class object utilizing an ArrayBuffer and the problem is encountered with methods that use #at and #put accessing the object's data fields. Most of my errors stating my object doesn't understand the #at or #put or mutable violation.

Sincerly,
Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: Problems creating class of mutable arrayBuffers objects

Postby Franck » 16 Feb 2017 22:23

Some comments :
- Class attributes must be separated by #, . You didn't put one between n and values.
- Writing "at()" or "put()" is wrong. You never use () withour parameters.

Here is a class Foo (this is theorical, ArrayBuffer should do the job) :

Code: Select all
Object Class new: Foo( n, mutable values )

Foo method: initialize( n -- )
   n := n
   ArrayBuffer newWith( n , 0 ) := values ;

Foo method: at       \ i -- x 
   @values at ;

Foo method: put      \ i x --
   @values put ;


Franck
Franck
 
Posts: 144
Joined: 29 Oct 2014 19:01


Return to General

Who is online

Users browsing this forum: No registered users and 3 guests