03-24-2018, 01:57 PM
(This post was last modified: 03-24-2018, 01:57 PM by DomesticAnt.
Edit Reason: Fixing a silly typo
)
You can encapsulate your arguments into classes using multiple constructors. It's still fairly ugly, but rather than scaling exponentially on the number of arguments the complexity scales linearly on the number of types of argument.
Here's a trivial example using two arguments of the same type (a location specified as an object, a point or a category):
You can see that the overloaded Thing() constructors effectively convert each argument to a Point as the time that the function is called, so I don't have to overload the function at all. If I wanted to add a fourth acceptable argument type, I'd just add another Thing constructor.
Here's a trivial example using two arguments of the same type (a location specified as an object, a point or a category):
Code:
public class Thing {
void Thing( point np ) {
p = np;
}
void Thing( object thething ) {
p = thething.position;
}
void Thing( int category ) {
object thething = radar(category);
p = thething.position;
}
point p;
}
float distanceofthings( Thing a, Thing b ) {
return distance( a.p, b.p );
}
extern void object::Main()
{
object item = radar(Me);
point p = item.position;
float d = distanceofthings( new Thing(this), new Thing(p) );
message( "Distance to player avatar " + d );
d = distanceofthings( new Thing(this.position), new Thing( SpaceShip ));
message( "Distance to spaceship " + d );
}
You can see that the overloaded Thing() constructors effectively convert each argument to a Point as the time that the function is called, so I don't have to overload the function at all. If I wanted to add a fourth acceptable argument type, I'd just add another Thing constructor.