C
C#3y ago
Thinker

Interfaces containing static abstracts as type arguments

So curiously, this works
public interface IFoo
{
static abstract IFoo Create();
}
public interface IFoo
{
static abstract IFoo Create();
}
but these don't
public interface IBar
{
static abstract Task<IBar> CreateAsync();
}
public interface IBar
{
static abstract Task<IBar> CreateAsync();
}
public interface IBar<TSelf> where TSelf : IBar<TSelf>
{
static abstract Task<IBar<TSelf>> CreateAsync();
}
public interface IBar<TSelf> where TSelf : IBar<TSelf>
{
static abstract Task<IBar<TSelf>> CreateAsync();
}
The interface 'IBar' cannot be used as type argument. Static member 'IBar.CreateAsync()' does not have a most specific implementation in the interface. Is there any way to work around this without using a regular factory interface?
2 Replies
333fred
333fred3y ago
Just return TSelf from CreateAsync
Thinker
ThinkerOP3y ago
Oh yeah, right Yeah I'm slowly realizing this use-case isn't even that good

Did you find this page helpful?