Inheritance in JASS

Ayanami

칼리
Reaction score
288
Wanted to know how Inheritance exactly works in JASS. Let's say you have struct Parent and struct Child, I understand that the Child inherits instance members and methods only, right?

I'm confused about the constructor part. If you call [ljass]local Child c = Child.create()[/ljass], this calls Parent's [ljass].create[/ljass] method? Would that mean the variable c holds Parent's instance or Child's instance? I would like to know how exactly the constructor works for a Child & Parent.

Another question:

JASS:
struct Parent
endstruct

struct Child extends Parent
endstruct

function Test takes nothing returns nothing
    local Parent p = Child.create() // legal?
endfunction


If the above is legal, let's say I have a new scenario: struct Parent, struct Child1, struct Child2.

JASS:
struct Parent
    method print takes nothing returns nothing
        call BJDebugMsg("Hi")
    endmethod
endstruct

struct Child1 extends Parent
endstruct

struct Child2 extends Parent
endstruct

function Test takes nothing returns nothing
    local Parent p

    if GetRandomInt(0, 1) == 0 then
        set p = Child1.create()
    else
        set p = Child2.create()
    endif

    // anyway to check if p holds an instance of Child1 or Child2?
endfunction


And for the above scenario, would calling [ljass]p.print()[/ljass] result in an error?

Thanks.
 

Dirac

22710180
Reaction score
147
I'm almost certain that child structs have no allocation methods and take instead the parent's methods.
I advise you to avoid struct inheritance as it takes you nowhere plus you can't extend structs to other structs that extend array (which is really stupid)
 

Bribe

vJass errors are legion
Reaction score
67
1. Yes it is legal since Parent and Child share the same allocators.

2. [ljass]if p.getType() == Child1.typeid then[/ljass]
 

lep

Active Member
Reaction score
8
http://wc3c.net/vexorian/jasshelpermanual.html#extendstruct

1. Yes it is technically legal but since Parent and Child do not share the same allocators this is going to cause errors for you.
Uhm? They do.
JASS:
//Generated allocator of Parent
function s__Parent__allocate takes nothing returns integer
 local integer this=si__Parent_F
    if (this!=0) then
        set si__Parent_F=si__Parent_V[this]
    else
        set si__Parent_I=si__Parent_I+1
        set this=si__Parent_I
    endif
    if (this>8190) then
        return 0
    endif

    set si__Parent_type[this]=1
    set si__Parent_V[this]=-1
 return this
endfunction

//Generated allocator of Child2
function s__Child2__allocate takes nothing returns integer
 local integer this=s__Parent__allocate()
 local integer kthis
    if(this==0) then
        return 0
    endif
    set si__Parent_type[this]=3
    set kthis=this

 return this
endfunction


//Generated allocator of Child1
function s__Child1__allocate takes nothing returns integer
 local integer this=s__Parent__allocate()
 local integer kthis
    if(this==0) then
        return 0
    endif
    set si__Parent_type[this]=2
    set kthis=this

 return this
endfunction


But yeah, just read the [...] manual.
 

Bribe

vJass errors are legion
Reaction score
67
lep, you replied too quickly. I edited my post after I saw that child way extending parent.
 

Ayanami

칼리
Reaction score
288
So practically, this means Parent, Child1, Child2 all together can only have 8190 instances? That's kinda lame.
 

Bribe

vJass errors are legion
Reaction score
67
JASS:
struct parent[200000]


Of course this adds plenty of KB size to your map.
 

Dirac

22710180
Reaction score
147
So practically, this means Parent, Child1, Child2 all together can only have 8190 instances? That's kinda lame.
The way i see it this is the only way it makes sense, but after reading what lep posted i find struct extensions to other structs even more stupid.
There's always a way to avoid them
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
could you use a struct to return integers used as the arrays for other structs instances? essentially making it 8190^2 (67076100 instances)
 

Bribe

vJass errors are legion
Reaction score
67
First change your avatar, it gives me the creeps.

Second, not in the way you think. If you want big integers which can exceed 2^31 you can use BigInt created by Nestharus.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I know the question has been answered, but for questions like these, remember that you can always look at the JASSHelper Manual. http://www.wc3c.net/vexorian/jasshelpermanual.html#extendstruct

After showing a struct B that extends a struct A:

JASSHelper Manual said:
Internally, B.allocate() is actually calling A's constructor and B.destroy will also call B's deconstructor. If a base struct got a custom create method, the structs extending it will have to use it for allocate(). If the custom create method requires arguments, the allocate method for child structs will require the same arguments

There's a bit of Engrish in there, but you can pretty much get the gist of it.
 

Dirac

22710180
Reaction score
147
GFreak45 you still have a lot to learn about structs. Here i present you this example.
JASS:
struct Values
    
    integer number
    
endstruct

function Test takes nothing returns nothing
    
    local integer i = Values(6).number
    
endfunction
Is the same as
JASS:
struct Values
    
    integer number
    
endstruct

function Test takes nothing returns nothing
    
    local Values someValue = Values(6) // you can just write 6 here and it would work
    
    local integer i = someValue.number
    
endfunction
 

Dirac

22710180
Reaction score
147
Yes, to avoid interfaces use textmacros
to avoid inheritance, well, don't do it
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
whats the difference between .number/.create and all of the other .tags on the end of the set variable?
 

tooltiperror

Super Moderator
Reaction score
231
> Yes, to avoid interfaces use textmacros
I recommend you do not use textmacros for inheritance :)

I find that multiple inheritance is a very complicated and tricky concept, and I don't use it in any language. I do, however, support reopening structs (but Jass doesn't support this) and I think modules are great. You can just make a module replace a parent class and implement it, and change a few minor details.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top