[Mono-bugs] [Bug 25168] New - mcs doesn't realize implicit conversion operator

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
23 May 2002 11:30:31 -0000


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by detlev@die-offenbachs.de.

http://bugzilla.ximian.com/show_bug.cgi?id=25168

--- shadow/25168	Thu May 23 07:30:31 2002
+++ shadow/25168.tmp.14814	Thu May 23 07:30:31 2002
@@ -0,0 +1,127 @@
+Bug#: 25168
+Product: Mono/MCS
+Version: unspecified
+OS: 
+OS Details: SuSE 8.0
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: Misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: detlev@die-offenbachs.de               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: mcs doesn't realize implicit conversion operator
+
+The following code taken from "Programming C#" produces an mcs (v0.11)
+compiler error (csc compiles ok). It seems that mcs doesn't recognise the
+implementation of the type conversion function from int to Fraction. The
+error message received is
+
+ex6-01.cs(88) error CS0019: Operator + cannot be applied to operands of
+type `Fraction' and `int'
+
+The example code is as follows
+----------------------------------------------------------------
+using System;
+
+public class Fraction {
+
+  public Fraction(int numerator, int denominator) {
+    Console.WriteLine("In Fraction Constructor(int, int)");
+    this.numerator = numerator;
+    this.denominator = denominator;
+  }
+
+  public Fraction(int wholeNumber) {
+    Console.WriteLine("In Fraction Constructor(int)");
+    numerator = wholeNumber;
+    denominator = 1;
+  }
+
+// this function is not recognised by mcs further down
+  public static implicit operator Fraction(int theInt) {
+    Console.WriteLine("In implicit conversion to Fraction");
+    return new Fraction(theInt);
+  }
+
+  public static explicit operator int(Fraction theFraction) {
+    Console.WriteLine("In explicit conversion to int");
+    return theFraction.numerator /
+      theFraction.denominator;
+  }
+
+  public static bool operator==(Fraction lhs, Fraction rhs) {
+    Console.WriteLine("In operator ==");
+    if (lhs.denominator == rhs.denominator &&
+        lhs.numerator == rhs.numerator) {
+      return true;
+    }
+    // code here to handle unlike fraction
+    return false;
+  }
+
+  public static bool operator!=(Fraction lhs, Fraction rhs) {
+    Console.WriteLine("In operator !=");
+    return !(lhs == rhs);
+  }
+
+  public override bool Equals(object o) {
+    Console.WriteLine("In method Equals");
+    if (! (o is Fraction)) {
+      return false;
+    }
+    return this == (Fraction) o;
+  }
+  
+  public static Fraction operator+(Fraction lhs, Fraction rhs) {
+    Console.WriteLine("In operator +");
+    if (lhs.denominator == rhs.denominator) {
+      return new Fraction(lhs.numerator + rhs.numerator,
+        lhs.denominator);
+    }
+
+    // simplistic solution for unlike fractions
+    // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8
+    int firstProduct = lhs.numerator * rhs.denominator;
+    int secondProduct = lhs.denominator * rhs.numerator;
+    return new Fraction(
+      firstProduct + secondProduct,
+      lhs.denominator * rhs.denominator);
+  }
+
+  public override string ToString() {
+    String s = numerator.ToString() + "/" +
+      denominator.ToString();
+    return s;
+  }
+
+  private int numerator;
+  private int denominator;
+}
+
+public class Tester {
+  static void Main() {
+    Fraction f1 = new Fraction(3,4);
+    Console.WriteLine("f1: {0}", f1.ToString());
+
+    Fraction f2 = new Fraction(2,4);
+    Console.WriteLine("f2: {0}", f2.ToString());
+
+    Fraction f3 = f1 + f2;
+    Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString());
+
+    Fraction f4 = f3 + 5;                 // <-- mcs fails on this line
+    Console.WriteLine("f3 + 5 = f4: {0}", f4.ToString());
+
+    Fraction f5 = new Fraction(2, 4);
+    if (f5 == f2) {
+      Console.WriteLine("f5: {0} == f2: {1}",
+        f5.ToString(), f2.ToString());
+    }
+  }
+}
+-----------------------------------------------------------------