views:

82

answers:

1

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 ExistentialType
{
    private ExistentialType() { }


    public abstract int Foo();

    public ExistentialType Create()
    {
        return new ConcreateType1();
    }

    private class ConcreateType1 : ExistentialType
    {
        public override int Foo()
        {
            throw new NotImplementedException();
        }
    }

    private class ConcreateType2 : ExistentialType
    {
        public override int Foo()
        {
            throw new NotImplementedException();
        }
    }

    private class ConcreateType3 : ExistentialType
    {
        public override int Foo()
        {
            throw new NotImplementedException();
        }
    }
}

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.

+1  A: 

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.

Mystere Man