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


    public abstract int Foo();

    public 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.

Community
  • 1
  • 1
Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • I've had some people comment that one of the feature requests that I've made on the Microsoft .Connect site as an explanation of need for Existential Types. That is the first time I've heard of the phrase. https://connect.microsoft.com/VisualStudio/feedback/details/576675/feature-request-for-composable-generic-types-within-a-type-definition?wa=wsignin1.0#details – jpierson Dec 23 '10 at 02:04

2 Answers2

1

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.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

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.

Kannan Goundan
  • 4,962
  • 3
  • 24
  • 31