In Visual Basic, I want to create an interface that includes a function that returns an object of the implementing class. That is
public interface I
function maker as ???
end interface
public class X
implements I
function maker as X
return new X()
end function
end class
public class Y
implements I
function maker as Y
return new Y()
end function
end class
Is there a way to say that? I suppose I could always say that maker returns an I rather than an X or a Y, but then callers would have to cast it. I thought of defining the interface as
public interface I(of ii as I)
But the whole point in doing this was so I could create a generic class that uses an of I, and if I say that, then the compiler insists on an infinite regression of I(of I(of I...
Not exactly, but you can do this:
Or, like this:
Neither of these options force the derived class to return an instance of their own type, but they allow you to implement it that way.