473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple constraints in generics

Hi,

I just downloaded rotor and gyro and am playing a bit with generics. I
enclose a sample program. My question is, according to the language
spec (http://msdn.microsoft.com/vcsharp/te...e/default.aspx, C#
2.0 Specification), a syntax like:

public class MathEngine<T> where T: IHasX, IHasY {

}

should be valid. Yet, the rotor+gyro compiler complains. I have
attempted also

public class MathEngine<T> where T: IHasX, T: IHasY {

}

but it seems that it only picks up the last part, namely, only IHasY.

I know rotor+gyro are kind of experimental, but still, I'd like to
make sure I am not missing out obvious explanations. Are these
problems just bugs?

Regards,

Hung Jung
using System;

public interface IHasX {
int x {get;set;}
}

public interface IHasY {
int y {get;set;}
}

public class MathEngine<T> where T: IHasX, IHasY {
public void incX(T me) {
int x;
x = me.x;
x += 1;
me.x = x;
}
public void incY(T me) {
int y;
y = me.y;
y += 1;
me.y = y;
}
}

public class A: IHasX, IHasY {

int _x;
int _y;

public MathEngine<A> math;

public A() {
_x = 2;
_y = 3;
math = new MathEngine<A>() ;
}

public int x {
get {return _x;}
set {_x = value;}
}

public int y {
get {return _y;}
set {_y = value;}
}

public void calc() {
math.incX(this) ;
}

}

class MainApp {
public static void Main() {
A a;
a = new A();
a.calc();
Console.WriteLi ne("Values = {0}", a.x);
}
}
Nov 15 '05 #1
2 5804
Hung,

Well, you kind of explained it yourself. The Rotor/Gyro implementations
were created before the designs were confirmed for the next version of the
framework. Constraints might not have been in the original design for the
CLR when they did the Rotor/Gyro project.

I wouldn't expect the new version of the CLR/Framework to work with the
current version of Rotor/Gyro.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hung Jung Lu" <hu********@yah oo.com> wrote in message
news:8e******** *************** ***@posting.goo gle.com...
Hi,

I just downloaded rotor and gyro and am playing a bit with generics. I
enclose a sample program. My question is, according to the language
spec (http://msdn.microsoft.com/vcsharp/te...e/default.aspx, C#
2.0 Specification), a syntax like:

public class MathEngine<T> where T: IHasX, IHasY {
.
}

should be valid. Yet, the rotor+gyro compiler complains. I have
attempted also

public class MathEngine<T> where T: IHasX, T: IHasY {
.
}

but it seems that it only picks up the last part, namely, only IHasY.

I know rotor+gyro are kind of experimental, but still, I'd like to
make sure I am not missing out obvious explanations. Are these
problems just bugs?

Regards,

Hung Jung
using System;

public interface IHasX {
int x {get;set;}
}

public interface IHasY {
int y {get;set;}
}

public class MathEngine<T> where T: IHasX, IHasY {
public void incX(T me) {
int x;
x = me.x;
x += 1;
me.x = x;
}
public void incY(T me) {
int y;
y = me.y;
y += 1;
me.y = y;
}
}

public class A: IHasX, IHasY {

int _x;
int _y;

public MathEngine<A> math;

public A() {
_x = 2;
_y = 3;
math = new MathEngine<A>() ;
}

public int x {
get {return _x;}
set {_x = value;}
}

public int y {
get {return _y;}
set {_y = value;}
}

public void calc() {
math.incX(this) ;
}

}

class MainApp {
public static void Main() {
A a;
a = new A();
a.calc();
Console.WriteLi ne("Values = {0}", a.x);
}
}

Nov 15 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:<Oo******* *******@TK2MSFT NGP10.phx.gbl>. ..

Well, you kind of explained it yourself. The Rotor/Gyro implementations
were created before the designs were confirmed for the next version of the
framework. Constraints might not have been in the original design for the
CLR when they did the Rotor/Gyro project.

I wouldn't expect the new version of the CLR/Framework to work with the
current version of Rotor/Gyro.


Thanks. I also got confirmation from someone else on the
one-constraint limitation for the Gyro generics. It is actually also
mentioned in gyro.html in the unpacked distribution files.

regards,

Hung Jung
Nov 15 '05 #3

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

Similar topics

11
4001
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we can expect when Generics are released? Thanks,
12
6669
by: Dave Booker | last post by:
Is it possible to constrain a generic to be a value or primitive type? (Any kludges/workarounds welcome if the immediate answer is no....)
10
2287
by: Pierre Arnaud | last post by:
I'd like to provide two generic methods with the same name and the same arguments, but with different constraints, such as: void Explore<T>(T a, T b) where T : struct { } void Explore<T>(T a, T b) where T : class { } The value types should use the first method and the reference types the second method. However, this won't compile, as the C# compiler reports that there is already a method defined with the same name and the same...
4
1700
by: Ian Lazarus | last post by:
Why are constraints needed? In C++, an attempt to use a non-existing method will cause a compiler error. Isn't that true in C# also?
3
1365
by: Andrew Ducker | last post by:
Let's say I have an interface (IValid) I want to add to all the different Winform.Controls classes (textbox, radiobutton, etc). And I have a bunch of methods that then use these IValid Controls. Sometimes they access them as Controls, sometimes as IValids. Which means casting back and forth. You end up with things like: DoStuffWithControls(((Control)validControl).Controls); rather than DoStuffWithControls(validControl.Controls);
47
4034
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
6
5468
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class type constraint 'System.Windows.Forms.ComboBox' must come before any other constraints If I switch place of ComboBox with TextBoxBase, it would gripe:
5
1464
by: Fredo | last post by:
I'm new to Generics (years and years of VS.NET 2003 development, but very little .NET 2.0+). I have some routines for conversion from RGB to different color spaces and back. I would like the routines to support, at least, byte, int, and double types without having to create different versions of the routines. Is there any way to do this with generics? The problem is, if I do something like this: public static void RGB_to_YIQ<T>(T R, T...
1
1369
by: Aquarian | last post by:
I have a question in .NET Generics regarding the constraints. How would I prevent a value type from being used as the type parameter? say i have a generic class Foo class Foo<T> { }
0
9423
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
10216
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
10049
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
9997
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
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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...
0
5309
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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.