Public Group

# Multiple Types in Constructors?

This topic is 2803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Can't you just wrap doubles into Modules and/or provide implicit casts? That way you could treat everything as modules..

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites

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); } } 

##### Share on other sites
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').

##### Share on other sites

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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 24
• 44