Hi,
So here's a question that I've been thinking about for the last day or so, and I still don't have a great way of working around it. It's not mission-critical, but it'd be very useful if there was a good way to do this. Hopefully I'm just missing something obvious!
I'm currently trying to build a generic struct in HPC# that works differently depending on the type parameters you select. Since there are a few parameters involved, putting them all on the generic struct seems to make the most sense to me. The generic struct also has a few copies (depending on how many generic parameters you want). In order to use the appropriate type-specific function, I've constrained the generic type to require an interface, but since some of the methods on the generic type have no arguments, I don't actually have any instances of the constrained type. So currently, I can just forcibly instantiate a default one either in a field or in a variable to call the method on it. But that obviously seems like it will allocate a rather pointless (one-byte?) struct, then throw it away? Which doesn't seem right, of course. Here's a minimal example:
public struct TestStruct<TMeta>
where TMeta : ISomeInterface
{
public TMeta FakeField;
public int SomeFunction()
{
TMeta fakeVar = default;
fakeVar.GetValue(); // Option 1
return FakeField.GetValue(); // Option 2
}
}
public interface ISomeInterface
{
public int GetValue();
}
I do know that C# 11 would allow this through static abstract interface members, but this feature isn't available in HPC# currently.