HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Problems with inheriting methods.

12-28-2010, 01:18 AM#1
rogueteddybear
I have this small amount of code:

Hidden information:
Collapse JASS:
interface IBase
    method TestMethod takes nothing returns nothing defaults nothing
endinterface

struct Base extends IBase
    static method create takes nothing returns Base
        local Base b = Base.allocate()
        return b
    endmethod
    
    static method createAlternate takes nothing returns Base
        local Base b = Base.allocate()
        return b
    endmethod
endstruct

struct TestStruct extends Base
    method TestMethod takes nothing returns nothing
        call BJDebugMsg("I exist?")
    endmethod
endstruct


And I try to run this code:
Code:
local TestStruct t1 = TestStruct.create()
local TestStruct t2 = TestStruct.createAlternate()
call BJDebugMsg("Test 1 has TestMethod? "+B2S(t1.TestMethod.exists)+" Test 2 has TestMethod? "+B2S(t2.TestMethod.exists))

This results in it printing:
Quote:
Test 1 has TestMethod? true Test 2 has TestMethod? false

So the problem I'm having is t2 not recognizing that it has (or should have) TestMethod(). Is there something I can do to create t1 using create() and create t2 using another create function, but still have both of them inherit/overwrite everything correctly?
12-28-2010, 09:30 AM#2
0zyx0
TestStruct does inherit createAlternate(). BUT createAlternate() always return a struct of type Base. So t2 doesn't have TestMethod. create() however, doesn't get inherited, since TestStruct has its default constructor. I believe modules could be useful for what you are trying to do.
12-28-2010, 08:54 PM#3
rogueteddybear
Interesting, thanks for the reply.

Could I get a second opinion from someone else?
Is module the way to go?

Ultimately all i want is for t2 to have TestMethod without me doing anything other than calling createAlternate.
12-28-2010, 10:46 PM#4
Anitarf
This is tricky. vJass really has no support for alternate constructors. This is because calling .allocate on a child struct will always result in the parent's .create being called, there's no way to make .allocate call .createAlternate on the parent and you also can't just replace the .allocate call with .createAlternate because in that case, as you noticed, the struct is not considered to be of the child type.

In short, you must have your alternate constructors on the child struct. Whether modules are a way to work around this or not depends entirely on what you need this for in practice.
12-29-2010, 01:41 AM#5
rogueteddybear
Alright thanks. I guess I will just stick with the regular create.
12-31-2010, 10:09 AM#6
Bribe
Several ways to inherit properties from the struct. Delegates, typecasting and extending structs are the common ones. I could give examples, but it would help if you had a practical case to work with and I could provide you the most efficient way to achieve inheritance.
01-03-2011, 05:55 AM#7
rogueteddybear
Quote:
Originally Posted by Bribe
I could give examples, but it would help if you had a practical case to work with and I could provide you the most efficient way to achieve inheritance.

The example I posted is the simplest version of what I want to do.
I need t2 to recognize that it has TestMethod using t2.TestMethod.exists after using an alternate create function.