473,785 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler Error CS0702

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.

So my questions are:
Are there reasons why these types can't or shouldn't be supported as
constraints?
(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?
Are efforts being to made to support these types as constraints?

Jun 27 '08
35 2914
I'm not a language designer, so knowing what I would do is sort of
pointless. I don't have the kind of knowledge and experience that would
allow me to provide a reliable answer to that question. Unless you're an
experienced language designer, I doubt you do either, for what it's worth.
OK, I won't press you further. I'll just say that _designing_ something is a
lot easier than implementing it. :)

The only language I've designed and implemented so far is a simple scripting
language for a telnet client I wrote.

Jun 27 '08 #21
On Apr 18, 1:22 pm, PIEBALD <PIEB...@discus sions.microsoft .comwrote:
doing this in C# would mean removing:
o lock statements
o using statements
o foreach statements
o collection initializers
o type shorthands (string for System.String, int for System.Int32 etc)
.... and no doubt others that I can't think of off the top of my head

Well, you got me there; I can see how using, and foreach would be affected.
I'm not so convinced about lock, collection initializers, and type
shorthands, but I probably don't need to be.
I might as well fill in the details for interested parties:
o lock is equivalent to a try/finally with Monitor.Enter/Monitor.Exit
o collection initializers rely on IEnumerable and Add methods
o type shorthands are specified in terms of what framework type they
represent
(On another note; I really don't see what type shorthands add to the
language anyway other than syntactic sugar, but we needn't go there.)
They're *all* just syntactic sugar. There's an awful lot of syntactic
sugar in C#, particularly in C# 3. Don't assume that "syntactic sugar"
is a bad thing - it makes the language sweeter :)

Jon
Jun 27 '08 #22
On Apr 18, 1:48 pm, PIEBALD <PIEB...@discus sions.microsoft .comwrote:
I'm not a language designer, so knowing what I would do is sort of
pointless. I don't have the kind of knowledge and experience that would
allow me to provide a reliable answer to that question. Unless you're an
experienced language designer, I doubt you do either, for what it's worth.

OK, I won't press you further. I'll just say that _designing_ something is a
lot easier than implementing it. :)
I'd disagree with that, actually. Designing a fairly general purpose
language which balances power with readability, avoids brittleness, is
unambiguous etc is a very tricky thing indeed. The C# 3 designers have
(for the most part) painted an incredibly elegant picture. The
different language features all come together for LINQ, but each is
also individually useful outside LINQ. All this without ruining the
language in other ways.

Don't forget that a mistake in one version of the language is likely
to haunt future versions. (For example, I know my view that classes
should be sealed by default is also held by at least one of the C#
language designers - but even if *all* the designers agreed on that
now, there's no way on earth they'd change it at this point - it would
cause hideous amounts of breakage.)

Designing a very simple language may indeed be easier than
implementing it - but designing a general purpose language like C# is
a different matter.

Jon
Jun 27 '08 #23
On Fri, 18 Apr 2008 13:48:00 -0700, PIEBALD
<PI*****@discus sions.microsoft .comwrote:
OK, I won't press you further. I'll just say that _designing_ something
is a
lot easier than implementing it. :)
Yikes. Like Jon, I completely disagree with that. I have every
confidence that, given a correct design provided to me, I can implement
whatever that design might be. However, producing a correct design is
much more challenging.

I have no doubt that, if I wanted to, I could write a C# compiler. Today,
with the skills I have now. It might not be the best compiler around
(okay, it probably wouldn't be :) ), but it would work and it would comply
with the design.

But I am definitely not experienced enough to be designing an actual
language. The skills I currently have are inadequate to the task, and I
would either spend a huge amount of time (years) learning what I need to
know (either by trial and error, or through study of practices of other
language designers), or I would produce a very inferior design.

Frankly, having used a wide variety of languages over my career as a
programmer, I'm very impressed with C# even today (that is, now that the
honeymoon is over, so to speak :) ). I admit, I was a little put off
initially by its similarity to VB, but using C# gave me a greater
appreciation for some of the design choices made in VB, and I really like
how C# just "feels" simpler and more precise. Where it restricts the
programmer, it does so in ways that avoid too much inconvenience while
helping to prevent relatively common-but-serious bugs.

I don't think just anyone could come up with a language design that
provides all that. It's clear that some very smart people, looking very
hard at a wide variety of existing languages and their positive and
negative characteristics , came up with C# and while they may not always
have compromised in exactly the way everyone else would have, I always
feel confident that they made a particular compromise with the best
intentions and for good reasons. I've yet to see a counter-example to
that impression.

Pete
Jun 27 '08 #24
classes should be sealed by default

Ew, I think very few classes should be sealed; you just don't know what the
user wants to do with it.

However, I understand that things like sealed can help the compiler to
optimize the code, so I don't mind so much.

On the other hand, I think all classes should be partial by default.

Well, wait, no I _really_ think that there should be very few things that
are default.
For instance I don't think classes and members should have default access
modifiers, they should be explicit.
Well, basically I mean a sort of "ivory tower" language design (hmmm...
Pascal?), and I will admit that many real-world considerations must be made.

Jun 27 '08 #25
Don't assume that "syntactic sugar"
is a bad thing - it makes the language sweeter :)
Yes, but when the dessert trolley comes around and all I need is one thin
mint...

Jun 27 '08 #26
or I would produce a very inferior design.

Right (me too), but it would be easy. I agree that a _good_ design requires
a lot of real-world compromises.

And I certainly agree with the rest of that too; I liked C# from the time I
first read a spec of it circa 1999 (I was using C at the time).

Jun 27 '08 #27
On Apr 18, 2:51 pm, PIEBALD <PIEB...@discus sions.microsoft .comwrote:
classes should be sealed by default

Ew, I think very few classes should be sealed; you just don't know what the
user wants to do with it.
Search for "inheritanc e tax" on my blog: http://msmvps.com/jon.skeet

Inheritance is a powerful tool, but classes should be designed for
inheritance up front; inheritance tends to expose implementation
details (or at least make those implementation details important).
It's not so bad if none of the methods are virtual, but as soon as
they are, life becomes trickier.
However, I understand that things like sealed can help the compiler to
optimize the code, so I don't mind so much.
My reasons for preferring sealed classes have nothing to do with
optimisation, but instead with design.
On the other hand, I think all classes should be partial by default.
Why? Partial is only a compile-time notion within the same assembly. I
find it quite rare that I can create a new file, but not modify
another file contributing to the same assembly. So, the cost of making
a non-partial class partial later on is minimal.

In contrast, there's a cost to a class being partial when it doesn't
have to be. If I look at a class which is non-partial, I know
immediately that all the code for the class is in that same file;
there's no need to dig around elsewhere. If it's partial, I need to
find all the other source files before I can get a good idea of the
shape of the class.
Well, wait, no I _really_ think that there should be very few things that
are default.
For instance I don't think classes and members should have default access
modifiers, they should be explicit.
I'm in two minds about that, but I can certainly see its appeal. If we
*are* to have default access modifiers, I think C# takes exactly the
right approach by making everything as private as possible by default.
However, forcing developers to consider it to start with would also be
a good idea.
Well, basically I mean a sort of "ivory tower" language design (hmmm...
Pascal?), and I will admit that many real-world considerations must be made.
That's certainly true in many cases, but I suspect that it wouldn't
have caused too many complaints if C# had gone the "everything
explicit" route to start with.

Jon
Jun 27 '08 #28
OK, a different tack...

Is a type parameter constraint anything more than a compile-time type check?

The compiler can already check the type as far as being value type or
reference type, even when the type used is an enum. So compile-time type
checking of enums is possible.

namespace W
{
public enum X { X }

public class Y1<T{}
public class Y2<Twhere T : struct {}
public class Y3<Twhere T : class {}

public class Z
{
object V1 = new Y1<X>() ; // this succeeds
object V2 = new Y2<X>() ; // this succeeds
object V3 = new Y3<X>() ; // this fails (as it should)
}
}

I don't see a conceptual reason why "where T : enum" _shouldn't_ be supported.

Jun 27 '08 #29
On Apr 19, 7:00*am, PIEBALD <PIEB...@discus sions.microsoft .comwrote:
OK, a different tack...

Is a type parameter constraint anything more than a compile-time type check?
I suspect it's also checked at runtime.
The compiler can already check the type as far as being value type or
reference type, even when the type used is an enum. So compile-time type
checking of enums is possible.
Absolutely.
I don't see a conceptual reason why "where T : enum" _shouldn't_ be supported.
Likewise. I don't think any of us have come up with a reason why it
shouldn't be supported. I'd be interested to know the reason too - but
I don't think you'll find it here ;)

Jon
Jun 27 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8626
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to build my gui and dll projects, whose project, workspace, source files all resided on network drive mapped to H. The H mapping,
10
2568
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class forgets to implement some method I normally get a compile error(if the class is created in some way of course). When using the Visual Studio 6 compiler however, it does not generate a compile error in this case: - I have a implementing class A...
2
5840
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to create a dll, using the cl compiler I was getting many unresolved external errors, Example 1 below (8 in total, 7 to do with the registry function calls and 1 from wsprintfA call) as I hadn't included the AdvAPI32.lib file. So I created a LINK...
0
2402
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are closer to the point at which the
1
13253
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem,
3
5267
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 535F072A): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are...
4
3326
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The property 'DocItem' should be 'DocItems.' The problem is that instead of notifying me of where the problem has occurred, the compiler just crashes with an "internal error" (see bottom of this message). Now if I were to write: ...
6
2730
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep facing the same problem with many of them: I keep getting errors such as: ....\WinUser.h(8028): fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 2701)
27
3076
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in the source (gcc-4.3.1/gcc/c-common.c) of gcc 4.3.1, is a syntax error. I'm inclined to agree, as it is like no C I have ever met. what is "C_COMMON_FIXED_TYPES (, fract);" supposed to mean? Could it be
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.