Multiple Types in Constructors?

Started by
6 comments, last by SeraphLance 12 years, 2 months ago
I'm currently writing a noise library, and the other day I stumbled upon JTippetts' ANL in C. I noticed that his functions could take either doubles or other signals as inputs, which seems quite handy to have, rather than all the Flat(someDoubleHere) crap I have right now. Unfortunately, I ran into a bit of a snag trying to implement this.

for mutator functions, this is just a matter of having an overload, but it gets a bit tricky with constructors. For example:


public Blend(Module f1, Module f2, Module control)


I would need 2^3 constructors for this 3-input ctor alone. Some of my other constructors have as many as five inputs, and that's just an ugly mess. Is there any special kind of syntax for this? For example, something like (pseudocode):

Module F1, F2, Control;
double f1, f2, control;
bool m1, m2, m3; //denotes whether F1 or f1 is being 'used'.
public Blend(var f1, var f2, var control)
{
if(f1.getType() == typeof(Module))
{
F1 = f1;
m1 = true;
}
else
{
this.f1 = f1;
m1 = false;
}
. . .
}


I've discovered I could maybe use initializer lists for this purpose with a syntax like the following:


private Module otherVThing;
private double V;
public Object v
{
get{return V;}
set
{
if (value.GetType() == typeof(double))
{
V = (double)value;
}
else if (value.GetType() == typeof(Module))
{
otherVThing = (Module)value;
}
}
}

...

Foo a = new Foo{ arg1 = 4, arg2 = someModuleThingy, ... }


but this is a bit obtuse (if it works at all) and a massive amount of work for my existing codebase. Is there any easy way to get around this or should I just grin and bear it?
Advertisement
Can't you just wrap doubles into Modules and/or provide implicit casts? That way you could treat everything as modules..
First off... no you don't. You require MAYBE two constructors. Having one for each possible combination of doubles and Module is...to put it plainly: REALLY STUPID. Have one that takes just doubles, and one that takes just modules.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


First off... no you don't. You require MAYBE two constructors. Having one for each possible combination of doubles and Module is...to put it plainly: REALLY STUPID. Have one that takes just doubles, and one that takes just modules.


What if I wanted to create Blend(0.0, someFBM, someFunc) as per the header above?

Honestly, both the constructor explosion and the property feng shui are pretty much completely insane as far as I can tell, so I don't plan on doing either of them. However, the above syntax is pretty valuable to have for various reasons, so I don't want to entirely forsake it either.
Again, just create a DoubleModule which always returns the double. Look at this:

Blend((DoubleModule)0.0, someFBM, someFunc)

and then imagine writing a single constructor and single functions/methods/whatever, since you only need to handle the case where there are modules. You may not even need that cast, depending on implementation.

Again, just create a DoubleModule which always returns the double. Look at this:

Blend((DoubleModule)0.0, someFBM, someFunc)

and then imagine writing a single constructor and single functions/methods/whatever, since you only need to handle the case where there are modules. You may not even need that cast, depending on implementation.


Thanks. This led me to discover the syntax for user-defined casts. I have a "DoubleModule" already, that I call Flat. However, I'd like to be able to cast this implicitly. Unfortunately, it doesn't seem like I can do this, as implicit casts aren't allowed in interfaces.

If they were, I could have just done this:
public interface Module
{
...
public static implicit operator Module(double value)
{
return new Flat(value);
}
}
Ahh. No, they aren't.. Perhaps it would have been possible if you used a base class instead.. I'm not sure if a cast to a base class can return a derived class.

Still, that cast simply has to beat the alternative.

BTW, since you are using C#, you might want to prefix your interfaces with an 'I', since that's the usual notation (google 'MSDN Design guidelines for class libraries').

Ahh. No, they aren't.. Perhaps it would have been possible if you used a base class instead.. I'm not sure if a cast to a base class can return a derived class.

Still, that cast simply has to beat the alternative.

BTW, since you are using C#, you might want to prefix your interfaces with an 'I', since that's the usual notation (google 'MSDN Design guidelines for class libraries').


Module exists solely for the purposes of polymorphism, so changing to a base class went off without a hitch. Casting works fine too. LuaInterface doesn't like it, but I should be able to figure it out from here. Thanks a bunch.

This topic is closed to new replies.

Advertisement