Re: Compiler Error CS0702



On Thu, 17 Apr 2008 11:16:01 -0700, PIEBALD <PIEBALD@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

[...]
and I draw particular attention to the word "special". Of all the classes in
.net, very few are "special".

That's correct. And the "special" is IMHO important. They are telling you that it's not really a class. You can't inherit it, for example. It's a façade, one that exists to make particular aspects of the type more seamless. The type isn't _really_ a class.

An enum is still basically a value type...that is, it behaves more like a value type than a reference type, and it's reference types that can be used as a constraint.

The existence of System.Enum as a base class for enums should _enable_ its
use as constraints. I see no restriction in the specification to _disallow_
it.

Well, this seems like a classic example of how biases affect our reading of information. You want to constrain the generic to only support enums, so of course you're predisposed to not read the information in a way that would contradict that. On the other hand, my bias is towards explaining how the specification adequately describes what you're seeing, so I'm predisposed to reading the information in a way that does contradict your position.

Sorry to say, there's really not much way around that. The fact is, you can't constrain your type parameters to the System.Enum. You can insist that the specification doesn't address that issue all you want, it's not going to change how things work.

By the way, in the ECMA specification, they suggest using a static constructor for checking the constraint, for situations where the language doesn't support the constraint you want. That would at least ensure that the constraint is checked only once, when you first use the class with that specific enum type, rather than for each instance of the class you create.

Of course, if you're trying to constrain something other than a class, I guess that wouldn't apply. :)

Pete
.


Loading