Is it acceptable to have a dummy generic parameter in the parameters list in order to save method consumers from the need to specify type arguments? For example -
public T Generate<T>(int paramForInstance) where T : MyClass {
return MyClass.SomeFactoryMethod<T>(paramForInstance);
}
//calling code needs to specify type
MyDerivedClass instance = Generate<MyDerivedClass>(5);
public T Generate<T>(int paramForInstance, T dummy) where T : MyClass {
return MyClass.SomeFactoryMethod<T>(paramForInstance);
}
//calling code needs to provide a "sample" to enable type inference
MyDerivedClass instance = null;
instance = Generate(5,instance);
The assumption here is that if junior other devs on the team don't need to think about generics, they're less likely to kick.
A Little Background (feel free to skip this)
A couple weeks ago, a management-developer type in my team freaked out when he saw code (my code) that looked like this:
//conversion methods, using generics
private static TResult? IfNotDangerous<TResult>
(SomeType firstThing, string name, Func<object, TResult?> conversion)
where TResult : struct
{
return IfNotDangerous(firstThing, name,
(any, dummy) => conversion(any), (TResult?)null);
}
private static TResult IfNotDangerous<TResult>
(SomeType firstThing, string name, Func<object, TResult, TResult> conversion,
TResult defaultValue)
{
if (firstThing == null) return defaultValue;
if (!firstThing.Contains(name)) return defaultValue;
if (firstThing[name] == DangerousValue) return defaultValue;
return conversion(firstThing[name], defaultValue);
}
The complaint he had was, "Why are you using a struct?! That's so resources-heavy! And what does 'where' mean? Nobody on the senior development end knows what that does - I've asked them all!"
After overcoming my self-pity, I found a way around that didn't use generics for this particular method. Appeasement was achieved. But I'd like to avoid trouble in the future, if possible.
EDIT
I appreciate all the encouragement, but the question was not "should I look elsewhere for work?" - it was "is this a usable workaround?"