• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Things about interface.

Status
Not open for further replies.
Level 23
Joined
Apr 16, 2012
Messages
4,041
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)
 
Level 11
Joined
Oct 11, 2012
Messages
711
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.
 
Level 11
Joined
Oct 11, 2012
Messages
711
yea lol, I copy pasted it, forgot to rename it, it should be struct A
Alright. :) Why not just do this:
JASS:
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

Edit: I kinda get the idea. LOL, thnx
 
Last edited:
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"
 
Level 11
Joined
Oct 11, 2012
Messages
711
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?

Edit: BTW, class means struct, right?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
as Purge said, this is vJass way of polymorphism.

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
 
Level 11
Joined
Oct 11, 2012
Messages
711
as Purge said, this is vJass way of polymorphism.

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

Thanks a lot. +Rep
 
Status
Not open for further replies.
Top