473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.WriteLine("Values = {0}", a.x);
}
}
Nov 15 '05 #1
2 5779
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.com

"Hung Jung Lu" <hu********@yahoo.com> wrote in message
news:8e**************************@posting.google.c om...
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.WriteLine("Values = {0}", a.x);
}
}

Nov 15 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<Oo**************@TK2MSFTNGP10.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
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...
12
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
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...
4
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
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. ...
47
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...
6
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...
5
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...
1
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.