[svn] r6443: nemerle/trunk/ncc: external/InternalTypes.n generation/Typer3.n testsuite/positive/delegate.n

nazgul svnadmin at nemerle.org
Sun Jul 9 22:52:41 CEST 2006


Log:
Allow conversion from local functional value to delegate

Author: nazgul
Date: Sun Jul  9 22:52:36 2006
New Revision: 6443

Modified:
   nemerle/trunk/ncc/external/InternalTypes.n
   nemerle/trunk/ncc/generation/Typer3.n
   nemerle/trunk/ncc/testsuite/positive/delegate.n

Modified: nemerle/trunk/ncc/external/InternalTypes.n
==============================================================================
--- nemerle/trunk/ncc/external/InternalTypes.n	(original)
+++ nemerle/trunk/ncc/external/InternalTypes.n	Sun Jul  9 22:52:36 2006
@@ -323,6 +323,14 @@
     get { void_tycon }
   }
 
+  public GetMethodWithReturnType (ret_type : TyVar) : IMethod
+  {
+    if (ret_type.Fix () is MType.Void)
+      apply_void
+    else
+      apply
+  }
+  
   public static Make (ty : TyVar) : MType.Class
   {
     match (ty.Fix ().FunReturnTypeAndParms ()) {

Modified: nemerle/trunk/ncc/generation/Typer3.n
==============================================================================
--- nemerle/trunk/ncc/generation/Typer3.n	(original)
+++ nemerle/trunk/ncc/generation/Typer3.n	Sun Jul  9 22:52:36 2006
@@ -1405,6 +1405,18 @@
           | TExpr.MethodRef (obj, meth, typarms, nonvirt) =>
             (Walk (obj), TExpr.MethodAddress (obj.Type, meth, !nonvirt, typarms))
 
+          | TExpr.LocalRef (decl) =>
+            def mtype = decl.Type.Fix();
+           
+            match (mtype.FunReturnTypeAndParms ()) {
+              | Some ((parms, ret_type)) =>
+                def ft = InternalType.GetFunctionType (parms.Length);              
+                def meth = ft.GetMethodWithReturnType (ret_type);
+                (parm, TExpr.MethodAddress (mtype, meth, true, []))
+              
+              | _ => Util.ice ("nonfunction type?")
+            }
+
           | TExpr.DefFunctionsIn ([func], LocalFunRef (decl, typarms))
             when func.decl.Equals (decl) => // this is for sure lambda
 
@@ -1449,11 +1461,7 @@
 
       def plain_call () {
         def ft = InternalType.GetFunctionType (parms.Length);
-        def meth =
-          if (ret_type.Fix () is MType.Void)
-            ft.ApplyVoidMethod
-          else
-            ft.ApplyMethod;
+        def meth = ft.GetMethodWithReturnType (ret_type);
         just_call (meth, TExpr.MethodRef (func.ty, Walk (func), meth, [], false), [])
       }
 

Modified: nemerle/trunk/ncc/testsuite/positive/delegate.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/delegate.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/delegate.n	Sun Jul  9 22:52:36 2006
@@ -204,6 +204,41 @@
 }
 }
 
+module FunctionToDelegate
+{
+  public Sort[T] (sourse : array [T], comparison : T * T -> int) : void
+  {
+    System.Array.Sort(sourse, comparison);
+  }
+
+  public Convert[I,O] (sourse : array [I], conv : I -> O) : array [O]
+  {
+    System.Array.ConvertAll(sourse, conv);
+  }
+
+  public ForEach[T] (sourse : array [T], act : T -> void) : void
+  {
+    System.Array.ForEach (sourse, act);
+  }
+  
+  print_arr ['a] (arr : array ['a]) : void
+  {
+     foreach (x in arr) System.Console.Write ("{0}, ", x);
+     System.Console.WriteLine ();
+  }
+    
+   public Run() : void
+   {
+     def my_arr = array [6,2,8,32,7,333,2,9,1,-3,-33,43];
+     Sort (my_arr, _ - _);
+     print_arr (my_arr);
+     def str_arr = Convert (my_arr, fun (x) { (x + 1).ToString () });
+     print_arr (str_arr);
+     ForEach (str_arr, System.Console.Write : string -> void);
+   }
+}
+      
+
 
 public module M {
 
@@ -303,6 +338,7 @@
     assert (_gen_d (2) == 1);
     Tester.Mai ();    
     MyClass.Run ();
+    FunctionToDelegate.Run ();
   }
 }
 
@@ -330,5 +366,8 @@
 A::Hello
 B::World
 C::!
+-33, -3, 1, 2, 2, 6, 7, 8, 9, 32, 43, 333, 
+-32, -2, 2, 3, 3, 7, 8, 9, 10, 33, 44, 334, 
+-32-2233789103344334
 END-OUTPUT
 */



More information about the svn mailing list