{ int id; } struct Thing { public Id id; } ...">

Why do I get a "loop in structure structure" with phantom types in C #?

Example:

struct Id<T> { int id; } struct Thing { public Id<Thing> id; } 

this causes a cyclic structure structure, but I do not see the cycle. if Id has a field of type T, sizeof will be undefined, but it is not.

Is it a mono error or part of the specification?

+6
source share
1 answer

As discussed in the comments, although this code is compiled using the MS C # compiler, it does not actually execute - it gives a TypeLoadException at runtime. Please note that the problem only occurs if both types are struct . So the question is, is this problem a C # compiler or runtime?

Since runtime is also covered by its own specification, I went through all parts of the CLI specification, which were even vagual, and I did not find anything that could prohibit this. Not in the definition of IL (obviously, since IL is considered valid), but not in runtime metadata structures.

Given this, I am more inclined to cause the implementation of the runtime to be erroneous. I suspect that when the Mono team encountered this problem, they decided to add a compiler error so that this situation was a lesser evil. Or maybe they just incorrectly evaluate the constraint of a cyclic struct :)

It may even be possible that it was not used to crash at runtime, which makes the C # compiler even more legit. Of course I can not check it :)

Unfortunately, this means that you cannot use this convenient design. Either make sure that one of the types is a class , or you just need to make a different type for each of these IdOfSomething yours. Just glad the Mono C # compiler told you this before you found out at runtime: P

+1
source

Source: https://habr.com/ru/post/984797/


All Articles