Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
Hi, there. I just read the JassHelper manual and have a basic understanding of "interface". But how is it useful? I saw very few scripts in the spell section use "interface". Is there a typical example that needs "interface"?
usually you dont need to use interface. They would have some use, if they werent so badly implemented in underlying Jass script(trigger evaluations and executions all over the place).
As hinted in JassHelper manual, interfaces are very useful if you want to pass any struct type into function and make it require to have certain method inside it.
Example:
JASS:
interface myInterface
method run takes nothing returns nothing
endinterface
struct B extends myInterface
method run takes nothing returns nothing
call BJDebugMsg("A.run()")
endmethod
endstruct
struct B extends myInterface
method run takes nothing returns nothing
call BJDebugMsg("B.run()")
endmethod
endstruct
function test takes myInterface p returns nothing
call p.run()
endfunction
function test2 takes nothing returns nothing
local A a = A.create()
local B b = B.create()
call test(a) //prints A.run()
call test(b) //prints B.run()
endfunction
if you know language like C++, this is equivalent of having class interface for polymorphism, which defines pure virtual member functions and cannot be instanciated, much like in here(you cant instanciate myInterface).
Interfaces have a lot of power in them, but their implementation usually overweights its usefulness(lots of trigger evaluations, which is sad because with half decent compiler this could've been solved statically)
usually you dont need to use interface. They would have some use, if they werent so badly implemented in underlying Jass script(trigger evaluations and executions all over the place).
As hinted in JassHelper manual, interfaces are very useful if you want to pass any struct type into function and make it require to have certain method inside it.
Example:
JASS:
interface myInterface
method run takes nothing returns nothing
endinterface
struct B extends myInterface
method run takes nothing returns nothing
call BJDebugMsg("A.run()")
endmethod
endstruct
struct B extends myInterface
method run takes nothing returns nothing
call BJDebugMsg("B.run()")
endmethod
endstruct
function test takes myInterface p returns nothing
call p.run()
endfunction
function test2 takes nothing returns nothing
local A a = A.create()
local B b = B.create()
call test(a) //prints A.run()
call test(b) //prints B.run()
endfunction
if you know language like C++, this is equivalent of having class interface for polymorphism, which defines pure virtual member functions and cannot be instanciated, much like in here(you cant instanciate myInterface).
Interfaces have a lot of power in them, but their implementation usually overweights its usefulness(lots of trigger evaluations, which is sad because with half decent compiler this could've been solved statically)
Is there a typo in your example? I'm kinda confused. The first struct should be Struct A, right?
I have no CS background, but thanks for the explanation anyway.
struct A
method run takes nothing returns nothing
call BJDebugMsg("A.run()")
endmethod
endstruct
struct B
method run takes nothing returns nothing
call BJDebugMsg("B.run()")
endmethod
endstruct
function test takes nothing returns nothing
local A a = A.create()
local B b = B.create()
call a.run() //prints A.run()
call b.run() //prints B.run()
endfunction
You can do that, but edo's test function allows you to input any class that extends that interface and call it. Normally, you would have to make one test function for A and one for B. That is essentially the idea of polymorphism.
The interface: if something extends it, it basically says "hey struct, create this function"
When you call the interface function, such as in edo's test function: "hey struct, idk who the hell you are. You might be A, or you might be B. All I know is that you extend this interface, therefore you have this function, so run it yo"
You can do that, but edo's test function allows you to input any class that extends that interface and call it. Normally, you would have to make one test function for A and one for B. That is essentially the idea of polymorphism.
The interface: if something extends it, it basically says "hey struct, create this function"
When you call the interface function, such as in edo's test function: "hey struct, idk who the hell you are. You might be A, or you might be B. All I know is that you extend this interface, therefore you have this function, so run it yo"
Thanks for the reply. So basically by extending interface, I can run multiple structs by using the same function, right? As long as the method in the multiple structs are the same?
Basically when you define interface, you just create a set of rules(methods) and when you have function which takes this interface, you know for sure that this function must exist within anything that is passed into the function. From inside you dont know if it is A or B, you just know that it must implement all the methods that are defined inside the interface.
What I said is not completly true, because interface can look like this:
JASS:
interface myI
method run takes nothing returns boolean defaults false
endinterface
Im not 100% sure, only around 99.97% sure that this means that:
if I extend the interface, I may or may not define method run, and if I dont do so, it will always be equal to false, otherwise it will return whatever the method defined inside the struct returns.
Yes class and struct are identical(I call it class cause I use that in C++, but struct is very same in there, and it works very similarily to structs in vJass)
Now I can write:
JASS:
struct A extends myI
method run takes nothing returns boolean
call BJDebugMsg("you have requested A to call .run()")
return true
endmethod
endstruct
struct B extends myI
endstruct
function tester takes myI instance returns boolean
return instance.run()
endfunction
function runner takes nothing returns nothing
local A a = A.create()
local B b = B.create()
local boolean aReturned = tester(a) //will print the message and be true
local booelan bReturned = tester(b) //will be false
endfunction
so now I dont require all structs extending the interface to have the method, but if it doesnt have it, I will just return false
Basically when you define interface, you just create a set of rules(methods) and when you have function which takes this interface, you know for sure that this function must exist within anything that is passed into the function. From inside you dont know if it is A or B, you just know that it must implement all the methods that are defined inside the interface.
What I said is not completly true, because interface can look like this:
JASS:
interface myI
method run takes nothing returns boolean defaults false
endinterface
Im not 100% sure, only around 99.97% sure that this means that:
if I extend the interface, I may or may not define method run, and if I dont do so, it will always be equal to false, otherwise it will return whatever the method defined inside the struct returns.
Yes class and struct are identical(I call it class cause I use that in C++, but struct is very same in there, and it works very similarily to structs in vJass)
Now I can write:
JASS:
struct A extends myI
method run takes nothing returns boolean
call BJDebugMsg("you have requested A to call .run()")
return true
endmethod
endstruct
struct B extends myI
endstruct
function tester takes myI instance returns boolean
return instance.run()
endfunction
function runner takes nothing returns nothing
local A a = A.create()
local B b = B.create()
local boolean aReturned = tester(a) //will print the message and be true
local booelan bReturned = tester(b) //will be false
endfunction
so now I dont require all structs extending the interface to have the method, but if it doesnt have it, I will just return false
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.