Re: Compiler Error CS0702



On Thu, 17 Apr 2008 09:31:03 -0700, PIEBALD <PIEBALD@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I'd really like to be able to constrain a generic type to System.Enum or,
better, enum. But of course that results in "Compiler Error CS0702".

So far I've been checking the type parameter at runtime and throwing an
exception if the provided type is not an enum. That works, but it just
doesn't seem quite right.

After reading through section 4.4.4 of the C# 3.0 spec I see no reason why
constraining to System.Enum should not be allowed.

I guess I'm a bit confused about the relationship between C# version and specification version.

I'm looking at "Standard ECMA-334", 4th edition, June 2006. There's no "section 4.4.4". The document I'm looking at describes generic constraints on page 405, section 25.7. And it describes the allowable constraints to clearly exclude enums or other value types. The only primary constraints allowed are an actual reference (class) type, or the keywords "class" or "struct".

What is giving you the impression that the specification would allow an enum as a type constraint?

So my questions are:
Are there reasons why these types can't or shouldn't be supported as
constraints?

Well, I think the most obvious reason is that since value types can't inherit other value types, if you're constraining the class to allow only one specific value type, then there's no need for the class to be generic in the first place. You'd only ever be able to use that generic class with one specific type.

Since an enum is a value type, it would fall into that same reasoning.

(If so, the documentation of Compiler Error CS0702 ought to state them.)
Is this simply a limitation of the compiler rather than of the language?

See above. As I read it, this is clearly described as part of thelanguage.

Are efforts being to made to support these types as constraints?

Not to my knowledge. I don't see why any effort would be made, given the above.

Pete
.



Relevant Pages

  • Re: extending enum
    ... >> enum constants. ... >> superclass, plus add additional capabilities or constraints. ... casting is required when a conversion may fail ...
    (comp.lang.java.programmer)
  • Re: Compiler Error CS0702
    ... But of course that results in "Compiler Error ... supported as constraints? ... For example, if you write "enum ... Even more so than a generic constrained to just a single type, I don't see what the value in a generic constrained to a single value of a single type would be. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... But of course that results in "Compiler Error ... supported as constraints? ... This is basically because enum types aren't really inherited from the ... have MyEnumWrapper be a wrapper for a single value of that particular ...
    (microsoft.public.dotnet.languages.csharp)
  • Compiler Error CS0702
    ... But of course that results in "Compiler Error CS0702". ... exception if the provided type is not an enum. ... Are efforts being to made to support these types as constraints? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... And it describes the allowable constraints to clearly exclude enums or other value types. ... I don't see it anywhere on their web site. ... section 4.4.4 has to do with satistfying constraints when _using_ a generic. ... can you quote any text from the C# specification that directly leads you to believe that you should be able to specify an enum type as a constraint for a generic? ...
    (microsoft.public.dotnet.languages.csharp)

Loading