Changing instance "type"

Dirac

22710180
Reaction score
147
I've come up with a very odd need:
Lets say I have 3 structs that share the same allocator and each one has a method with the same name as the other two, takes the same arguments, but does different things.
Is there a way to change the type of the instance so it would call other's struct's method?

Example:
JASS:
globals
	real array value
endglobals

struct Addition extends array
	method math takes real v returns nothing
		set value[this] = value[this] + v
	endmethod
endstruct

struct Substraction extends array
	method math takes real v returns nothing
		set value[this] = value[this] - v
	endmethod
endstruct

struct Multiplication extends array
	method math takes real v returns nothing
		set value[this] = value[this] * v
	endmethod
endstruct

Imagine I have a instance of type "Addition" named "this", when I call this.math(3) it adds 3 to value[this] but then I want to substract or multiply it by another value without having to type the struct's name? As in
[ljass]call this.math(5)[/ljass] would now result in substraction or multiplication
 

luorax

Invasion in Duskwood
Reaction score
67
Typecasting would work perfectly with your example:

JASS:
function Main takes nothing returns nothing
    local Addition this=Addition.allocate()
    set value[this]=100
    call this.math(5) //105
    call Substraction(this).math(15) //90
    call Multiplication(this).math(.5) //45
endfunction
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
you could do something like this:
JASS:
library Example
    globals
	real array value
    endglobals
    
    struct Addition extends array
	method math takes real v returns nothing
            set value[this] = value[this] + v
    	endmethod
    endstruct
    
    struct Substraction extends array
	method math takes real v returns nothing
            set value[this] = value[this] - v
	endmethod
    endstruct
    
    struct Multiplication extends array
        method math takes real v returns nothing
            set value[this] = value[this] * v
        endmethod
    endstruct
    
    struct Division extends array
        method math takes real v returns nothing
            set value[this] = value[this] / v
        endmethod
    endstruct
    
    struct Math extends array
        Addition add
        Subtraction sub
        Multiplication mult
        Division div
    endstruct
endlibrary


just 1 struct that holds instances of all 3
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top