Multiple constraints in generics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hung Jung Lu

    #1

    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);
    }
    }
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Multiple constraints in generics

    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]
    - mvp@spam.guard. caspershouse.co m

    "Hung Jung Lu" <hungjunglu@yah oo.com> wrote in message
    news:8ef9bea6.0 312011214.16b70 e85@posting.goo gle.com...[color=blue]
    > 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);
    > }
    > }[/color]


    Comment

    • Hung Jung Lu

      #3
      Re: Multiple constraints in generics

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<OoiIyjEuD HA.1512@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
      >
      > 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.[/color]

      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

      Comment

      Working...