- Joined
- Aug 7, 2013
- Messages
- 1,338
Hi,
Suppose I am trying to create an object (in the sense of struct/class/interface) which has many different instantiations, as shown below.
While
However, suppose I also give each animal another method, one that is the same, but will have different arguments across each animal.
where argN is any valid vJASS parameter, and for each struct implementing anima, it is not necessarily true that animalA_foo_argN == animalB_foo_argN.
So now if I had an array of animals like this:
I would get an error, because animal interface only accepts a single unique set of parameters for a method name.
So I cannot use an interface for this kind of polymorphism. What about extending an animal struct?
Well, it will run into the same problem--while I can stub a parent method foo(), the arguments still have to be the same.
So what should I do? I think one thing might be to make each of the possible foos stub methods in a parent struct like so
and then only know to call/implement the appropriate method, e.g.
Any better solution/thoughts? One downside is if I have N child structs, each one will essentially inherit N-1 methods they cannot/should never use. A waste of space, but it allows me to do stuff like
Suppose I am trying to create an object (in the sense of struct/class/interface) which has many different instantiations, as shown below.
JASS:
struct ant
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
struct bear
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
struct clam
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
While
ant, bear, clam
have different implementations of eat, they all essentially are the same object: an animal. Normally the solution would be simple: create an interface animal and give it an eat method. Then have each of the structs implement that interface for some OOP. However, suppose I also give each animal another method, one that is the same, but will have different arguments across each animal.
JASS:
struct ant implements animal
method foo takes nothing returns nothing
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
struct bear implements animal
method foo takes arg1 returns nothing
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
struct clam extends animal
method foo takes arg1, arg2, arg3 returns nothing
…
method eat takes unit food returns nothing
//an implementation specific to this animal
…
endmethod
endstruct
where argN is any valid vJASS parameter, and for each struct implementing anima, it is not necessarily true that animalA_foo_argN == animalB_foo_argN.
So now if I had an array of animals like this:
JASS:
animal array animals[3] //where 0 is an ant, 1 is a bear, and 2 is a clam
…
animals[1].eat(deer)
//no error, the interface method takes a single argument of type unit
animals[0].foo()
//error, only one implementation of method foo possible
I would get an error, because animal interface only accepts a single unique set of parameters for a method name.
So I cannot use an interface for this kind of polymorphism. What about extending an animal struct?
Well, it will run into the same problem--while I can stub a parent method foo(), the arguments still have to be the same.
So what should I do? I think one thing might be to make each of the possible foos stub methods in a parent struct like so
JASS:
struct animal
…
stub method fooAnt takes nothing returns nothing
endmethod
stub method fooBear takes arg1 returns nothing
endmethod
stub method fooClam takes arg1, arg2, arg3 returns nothing
endmethod
…
endstruct
and then only know to call/implement the appropriate method, e.g.
JASS:
struct ant extends animal
method fooAnt takes nothing returns nothing
//an implementation specific to ant
...
endmethod
endstruct
Any better solution/thoughts? One downside is if I have N child structs, each one will essentially inherit N-1 methods they cannot/should never use. A waste of space, but it allows me to do stuff like
JASS:
animal array animals[3]
set animals[0] = ant.create()
call animals[0].antFoo() //ok, no error here