Browse Source

[cs] Fixed toString() implementation to only override native ToString if the types match. Fixed Issue #874

Caue Waneck 13 years ago
parent
commit
2bd142f17e
2 changed files with 20 additions and 10 deletions
  1. 19 9
      gencs.ml
  2. 1 1
      std/cs/_std/haxe/lang/Exceptions.hx

+ 19 - 9
gencs.ml

@@ -1151,15 +1151,25 @@ let configure gen =
         newline w
         newline w
       end
       end
     end;
     end;
-    if PMap.mem "toString" cl.cl_fields && not (List.mem "toString" cl.cl_overrides) then begin
-      (* FIXME we need to check for compatible type first *)
-      write w "public override string ToString()";
-      begin_block w;
-      write w "return (string) this.toString();";
-      end_block w;
-      newline w;
-      newline w
-    end
+    (try
+      let cf = PMap.find "toString" cl.cl_fields in
+      (if List.mem "toString" cl.cl_overrides then raise Not_found);
+      (match cf.cf_type with
+        | TFun([], ret) ->
+          (match real_type ret with
+            | TInst( { cl_path = ([], "String") }, []) ->
+              write w "public override string ToString()";
+              begin_block w;
+              write w "return this.toString();";
+              end_block w;
+              newline w;
+              newline w
+            | _ -> 
+              gen.gcon.error "A toString() function should return a String!" cf.cf_pos
+          )
+        | _ -> ()
+      )
+    with | Not_found -> ())
   in
   in
 
 
   let gen_class w cl =
   let gen_class w cl =

+ 1 - 1
std/cs/_std/haxe/lang/Exceptions.hx

@@ -23,7 +23,7 @@ import system.Exception;
 		return obj;
 		return obj;
 	}
 	}
 	
 	
-	public function toString()
+	public function toString():String
 	{
 	{
 		return "Haxe Exception: " + obj;
 		return "Haxe Exception: " + obj;
 	}
 	}