Author Topic: Super Super Class  (Read 1775 times)

I'm playing around with super classes for a project I'm working on, and I've run into a problem. I can't use a namespace as a superclass said namespace already has a superclass.

Example:
Code: [Select]
$a = new ScriptObject() { class = A; };
$b = new ScriptObject() { superclass = A; class = B; };
$c = new ScriptObject() { superclass = B; class = C; };

That last new script object ($c), throws an error:


Error: cannot change namespace parent linkage for B from A to ScriptObject.


Is there a way around this issue? I'd like to be able to have lengthy parent hierarchies.
« Last Edit: April 15, 2017, 03:31:00 PM by Boomshicleafaunda »

First and foremost: You are missing two semicolons.

Second of all: Not sure if topic title is misleading. I first thought you meant to have an object with three namespaces, which is possible.

Finally: Is this an XY problem? What are you really trying to achieve?

he's clearly trying to open a black hole by making infinite super classes

Have you tried naming them and see if the issue goes away?

Second of all: Not sure if topic title is misleading. I first thought you meant to have an object with three namespaces, which is possible.

Have you tried naming them and see if the issue goes away?

I'm aware that I can do this:

Code: [Select]
$a = new ScriptObject() { class = A; };
$b = new ScriptObject() { superclass = A; class = B; };
$c = new ScriptObject(C) { superclass = A; class = B; };

But then can I get a fourth level of inheritance?

I tried the following to no avail:

Code: [Select]
$d = new ScriptObject() { superclass = C; };
$d = new ScriptObject(D) { class = C; };

Both of which resulted in the follow error (similar to OP):


Error: cannot change namespace parent linkage for C from B to ScriptObject.




Finally: Is this an XY problem? What are you really trying to achieve?

Essentially what PhantOS said. I'd like to be able to have as many inheritances as I want. If this isn't possible, then what's the maximum? Three?

Not sure why they have the same issues as GUIs do with namespaces.

You are overwriting $d which therefor it no longer has a super class, which you are changing its class and that is why you get the error (I may be wrong, just a theory)
« Last Edit: April 15, 2017, 03:36:13 PM by Kyuande »

I might have misunderstood namespaces and inheritance completely. I thought that it was just calling that namespace in a specific order and that other objects didn't have anything to do with it.

Anyway, when it comes to these kinds of limitations, you have to move around them. Either you are trying to complicate something too much, or you haven't thought about other solutions. I'm not saying that what you are trying to do is wrong, but I guess that you might find a better solution if you look from an another perspective.

Hey, welcome back!

You can use the object's actual name as another "layer" of inheritance in addition to the class and superclass fields, I believe.

correct, but only for scriptobjects/scriptgroups/tcp/htmlobject i think

You are overwriting $d which therefor it no longer has a super class, which you are changing its class and that is why you get the error (I may be wrong, just a theory)

I wasn't saying to run both statements. I meant that executing either statement results in that error. Sorry for any confusion.



Is there any known way to get a fourth level of inheritance?

I know that I could technically use a wrapper, but Torque Script isn't really suited for such a thing.

Example:

Code: [Select]
$a = new ScriptObject() { class = A; };
$b = new ScriptObject() { superclass = A; class = B; };
$c = new ScriptObject(C) { superclass = A; class = B; };
$d = new ScriptObject() { parent = $c; class = D; };

function D::someMethod(%this, %arg)
{
    return %this.parent.someMethod(%arg);
}


But that doesn't quite carry the same effect. I'd have to know every method name, and anyone who extends A, B, or C, would have to know to add a similar wrapper method to D. Not to mention the potential variables used outside of accessors and mutators.

Some languages off a general catch-all method (like PHP's magic __call method), where wrappers are really easy.

Is there any known way to get a fourth level of inheritance?

It would really help if you post what you are actually trying to do here. You generally don't need 4 levels of inheritance in a scripting language, there's most likely a better way.

As far as I know torkscript can't have more than 3 levels of inheritance (the superclass field -> the class field -> the name of the object). You can't cheat this system by trying to link more of them using multiple objects.

Not to mention the potential variables used outside of accessors and mutators.

Also, variables are always stored in a hashtable per-object. There's no such thing as "class fields" in ts.

...you generally shouldn't make accessor methods either, as they just slow the whole thing down with no benefit.
Only make methods if you need them to actually *do* something other than read a value and return it.
« Last Edit: April 18, 2017, 08:53:35 AM by Zeblote »