::SHE+ILA:: Records, Fields, and Classes [$84]

67
2
  • Squishy Plushie's avatar Artist
    Squishy Pl...
  • Prompt
    Read prompt
  • DDG Model
    DaVinci2
  • Access
    Public
  • Created
    19h ago
  • Try

More about ::SHE+ILA:: Records, Fields, and Classes [$84]

As previously mentioned, we'll now take a look at structures, also called Classes (C++, Java), Records (Pascal, Cobol), or (in Fortran) Named Common Blocks. Here we will use the more recent convention and call them Classes.
A Class in ::SHE+ILA:: is implemented as an Array whose fields (aka members) are named and accessed with a dot, rather than numbered and accesses with a subscript. For example :
Foo <:: (name="Fred", address="6502 Wanderland Avenue", postcode="KT88 6V6");
This defines a class called Foo and also adds the first record. You will notice the similarity with the local variable declaration in a function : a function's local variables are held on the stack in a local class in order to simplify the use of recursive functions. Each time the function calls itself, it creates a new "instance" of its locals, so they are independent of its other calls to itself.
Anyway, in Object Oriented Programming, a Class can contain "methods" (functions which operate on the data fields of the Class) as well as the data fields ("members") themselves. This is called Encapsulation, and prevents that data being accidentally changed from outside the Class.
Now, let's use the class Foo we've just defined :
ttyout(Foo[1].name, " lives at ", Foo[1].address, CRLF, Foo[1].postcode);
Let's add a method to Foo :
Foo << (details={ttyout(name, " lives at ", address, CRLF, postcode)});
We have used the insert (<<) operator to append an extra field "details" to Foo, which contains a lambda function printing the current record. Now, let's add another "instance" of Foo :
Foo[2] <:: ("Squishy", "386 Wanderland Road", "W00 YAY");
and display it :
Foo[2].details();
We can also "instantiate" an instance thus :
Bar <-:: Foo;
Bar will now be an exact copy of Foo except it doesn't contain the symbol table ("name", "address", "postcode", "details") but instead a protected pointer to the one within its original (Foo), and similarly the "methods" (functions) point to the originals too. This optimises the extra memory which would be used by the redundant copies of them. (C++ does it slightly differently : it uses "vtables" (which Microsoft claims to have patented) so to stay clean-room we do it differently :) )
We can also use "<-::" (instantiate) to derive a new class from Foo :
Woo <-:: Foo; Woo.details <:: {ttyout("My name is ", name, " and I live at ", address, CRLF, postcode)};
We have thus "overridden" the details method in our derived Class. Now let's add another field :
Woo[0] << ("email"); Woo[2] << ("squishyplushie137@yahoo.co.uk");
We have deliberately left Woo[1] unaltered.
We can still, by the way, treat our data structures as arrays : ttyout(Foo[0][1]); prints "name" etc.
Note that the method bodies are held in instance [1] (yet apply thoughout) which is why we created the initial record.

Comments


Loading Dream Comments...

Discover more dreams from this artist