Question

This example gives a "The type or namespace name 'MyType' could not be found (are you missing a using directive or an assembly reference?)"

using MyType = System.Func<System.Int32, System.Tuple<System.Int32, MyType>>;

Is it at all possible to declare a recursive type like this?

Était-ce utile?

La solution

No, I don't think it is possible. The right-hand side of your using statement needs to resolve to a real type before you can assign an alias to it. In your case, in order to resolve the right-hande side, the compiler must fully define the allias... which requires it to resolve the right-hand side. This recursive problem has no ending, so the compiler is clearly not going to bother.

To make the problem here more clear: lets assume the compiler managed to compile your alias, and I did this:

MyType mytype = x => Tuple<int, MyType>.Create(x, ???);

What could I possible put in the body of the function to define the return value? Eventually I need to have a constructable type somewhere to return.

Autres conseils

Assuming that you are talking about a using directive (at the top of a file) and MyType exists, yes, it is possible. For instance, this is perfectly legal:

using System;
using String = System.Func<System.String>;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String myString = () => "Foo";
            // myString is now a function returning the string "Foo".  Yikes.
        }
    }
}

That said, it would never pass a code review by me. Talk about confusing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top