Computer Science Canada Array of a class that accesses the array |
Author: | ttm [ Tue Jan 03, 2012 12:44 pm ] | ||||||
Post subject: | Array of a class that accesses the array | ||||||
Problem==== I have an array of some class Item, and I would like to have some method of Item be able to access elements of the array. Basically I would like to find a way to declare a prototype to a class. For example,
This obviously doesn't work, because class Item is declared after array ArrayOfItems Non-Solution==== According to the turing documentation (look up the page for keyword "type"), this is supposed to work:
The problem is when you type this, turing throws a "Type 'PtrItem' has not been resolved" when the documentation explicity states that it should be: Quote: A forward type allows pointers to be declared to the type before the type is resolved. To resolve a type, you must follow a forward with a declaration of the same name and in the same scope. This type declaration must include a typeSpec. Crappy workaround==== The only way I can think of to get this to work is:
... which is extremely ugly. It requires declaring 2 classes. It requires inheritance when there should be none. It requires the methods of the class to be split into 2 places in the code. It also requires items of ArrayOfItem to be explicitly casted to Item before usage. But it gets the job done. I there probably isn't a better way to do this but any insight is appreciated. |
Author: | DemonWasp [ Tue Jan 03, 2012 1:12 pm ] |
Post subject: | RE:Array of a class that accesses the array |
You should be able to do this with implement and implement by according to ericfourfour here: http://compsci.ca/v3/viewtopic.php?t=10908&highlight=turing+class+forward+declaration However, I wasn't able to get even his code sample working (complains about a syntax error at "implement") in Turing 4.1. I'm pretty sure that this feature existed in Turing 3.1.1, because I remember using it; I suspect support for that feature got broken at some point in Turing 4, and nobody noticed because Turing's OOP mechanisms are already so broken. My only advice is to switch to a more useful language. There are literally dozens of languages that have functional OOP integrated, including several you seem to know, based on your other posts. |
Author: | Dreadnought [ Tue Jan 03, 2012 3:39 pm ] | ||||||||||||
Post subject: | Re: Array of a class that accesses the array | ||||||||||||
ttm wrote: According to the turing documentation (look up the page for keyword "type"), this is supposed to work:
The problem is when you type this, turing throws a "Type 'PtrItem' has not been resolved" when the documentation explicity states that it should be: Quote: A forward type allows pointers to be declared to the type before the type is resolved. To resolve a type, you must follow a forward with a declaration of the same name and in the same scope. This type declaration must include a typeSpec. If you read your quote from the documentation if says "A forward type allows pointers to be declared to the type before the type is resolved". You created an array of PtrItem not ^PtrItem. What forward types allows you to do, is something like this.
To adress your problem. Like DemonWasp says you can achieve what you want with implement and implement by (they're not broken as DemonWasp suspects, but the classes/modules must be units for this to work). ericfourfour actually said this in the post DemonWasp refered to. ericfourfour wrote: There is more and I can expand on it if you want. The only thing you have to watch out for is that Foo can inherit but FooBody cannot. They also must be in different Turing Unit files.
Example: Item unit
ItemModule unit
ItemBody unit
Finally in the turing program
If you look through the Turing GUI files they use this kind of stuff. Also, a class with no specified inheritance inherits anyclass, so you could write your "crappy workaround" as
Hope this helps. |
Author: | DemonWasp [ Tue Jan 03, 2012 5:18 pm ] |
Post subject: | RE:Array of a class that accesses the array |
Hmm. You're definitely, Dreadnought, but I'm left with one thought: What the hell, Turing? This object-oriented stuff in Turing feels like it has several different designers, none of whom was really clear on what OOP was in the first place. This supposed solution (which is almost as clean as the same solution in C++), isn't actually what ericfourfour originally wrote. His caveat, which you quoted, notes that not only do you now have two classes and inheritance, but you have to have separate files too! Amazing. I'd rant more, but I suspect you know just how goofed up Turing's OOP is better than I do. |
Author: | ttm [ Tue Jan 03, 2012 10:32 pm ] |
Post subject: | Re: Array of a class that accesses the array |
Hmm. The new method using units and implement / implement by would work I suppose, but if so, it'd be even uglier than the original crappy workaround because in addition to using 2 classes, it also uses a module, keywords that are semi-broken, and is spread out between 3 separate files (I *hate* units). Also pointers to anyclass make me wince, but I suppose that's still better than the current crappy workaround. (Not that I need any of this in my code anywhere, I'm just speculating.) Anyways. I'd agree with DemonWasp 100% in how crappy Turing's OOP is, but then all the more merit in writing a good program in Turing. |