Re: Compiler Error CS0702



On Apr 17, 12:55 pm, PIEBALD <PIEB...@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
my bias is towards explaining how the specification adequately describes

what you're seeing

No, the specification doesn't. Certainly the specification allows the
keywords class and struct as constraints while (regrettably) omitting enum
(and delegate and event), so perhaps one can read _that_ as a statement in
your favor. But...

Enum and delegate could indeed be useful. (In particular, I'd like to
write a generic version of Enum.Parse to avoid the casts.)

Event would be a bizarre kind of constraint though, as "event" isn't a
kind of type - it would be like having a "property" constraint. What
would such a constraint mean?

From a language standpoint (not an implementation standpoint) I see no
reason to omit enum as a constraint in the first place.

If an implementer (and there _could_ conceivably be many implementations for
various platforms) chooses to implement enum as a class, then that class
_should_ be allowed as a constraint. But while Microsoft chose to use a class
to implement enums, for some unexplained reason, they also chose to make it
"special".

The specification guarantees that there will be a System.Enum class.
From the unified 3.0 spec:

<quote>
14.4 The System.Enum type

The type System.Enum is the abstract base class of all enum types
(this is distinct and different from the underlying type of the enum
type), and the members inherited from System.Enum are available in any
enum type. A boxing conversion (§4.3.1) exists from any enum type to
System.Enum, and an unboxing conversion (§4.3.2) exists from
System.Enum to any enum type.

Note that System.Enum is not itself an enum-type. Rather, it is a
class-type from which all enum-types are derived. The type System.Enum
inherits from the type System.ValueType (§4.1.1), which, in turn,
inherits from type object. At run-time, a value of type System.Enum
can be null or a reference to a boxed value of any enum type.
</quote>

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.

No, the specification knows nothing of System.Enum, that's merely a matter
of implementation.

Um, see the above.

If the MS spec isn't good enough for you, it's section 21.4 of the
ECMA spec (4th edition).

So yes, the specification most certainly *does* know about
System.Enum.

Jon
.



Relevant Pages

  • Re: Enums in a database
    ... a lookup table is more flexible than an enum because you can ... database constraint can be used to ensure that you'll never get a number in a ... public enum Specialty ... INSERT AutoMechanics SELECT 'Fred', 0 -- ForeignCars ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... enum as a type constraint? ... directly leads you to believe that you should be able to specify an enum ... type as a constraint for a generic? ... Provides the base class for enumerations. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... reason to omit enum as a constraint in the first place. ... If an implementer (and there _could_ conceivably be many implementations for ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... 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. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: enum for a column
    ... the closest thing to an enum in TSQL is a check constraint. ... > I have a column that should contain one of a fixed set of values. ... > size char and create a check constraint that tests for each possible value. ...
    (microsoft.public.sqlserver.programming)

Loading