开发者

Does this code describe an Existential Type in C#?

Currently watching Bart De Smet's explanation of IQueryable and he mentioned Existential Types (which I've been curious about for some time). After reading the answers to this question I'm just wondering if this is a way to construct it in C#:

public abstract class SomeExistentialType
{
    private SomeExistentialType() { }


    public abstract int Foo();

    pu开发者_开发问答blic ExistentialType Create()
    {
        return new ConcreteType1();
    }

    private class ConcreteType1: SomeExistentialType
    {
        public override int Foo()
        {
            //some implementation...
        }
    }

    private class ConcreteType1: SomeExistentialType
    {
        public override int Foo()
        {
            //some implementation...
        }
    }

    private class ConcreteType1: SomeExistentialType
    {
        public override int Foo()
        {
            //some implementation...
        }
    }
}

The idea is that if all of the concrete classes are defined as private nested classes (or maybe just internal classes) then you are forced to only use the interface.


Sounds a lot like the pImpl idiom.

I'm not a big fan of tricking the developer or forcing them to do something specific in these ways. In C/C++ i could understand the pImpl idiom's justification, because C++ lacks a lot of protections you get in C# and .net, but oh well.


No, that is not what an existential type is. In the answer's to the other question on existential types, look at the VirtualMachine example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜