Browse Source

better handling for default parameters values

Nicolas Cannasse 17 years ago
parent
commit
4c6392c8b9
3 changed files with 9 additions and 9 deletions
  1. 1 1
      ast.ml
  2. 1 1
      parser.ml
  3. 7 7
      typeload.ml

+ 1 - 1
ast.ml

@@ -155,7 +155,7 @@ and type_path =
 	| TPExtend of type_path_normal * (string * bool option * anonymous_field * pos) list
 
 type func = {
-	f_args : (string * bool * type_path option * constant option) list;
+	f_args : (string * bool * type_path option * expr option) list;
 	f_type : type_path option;
 	f_expr : expr;
 }

+ 1 - 1
parser.ml

@@ -399,7 +399,7 @@ and parse_fun_param = parser
 	| [< name = any_ident; t = parse_type_opt; c = parse_fun_param_value >] -> (name,false,t,c)
 
 and parse_fun_param_value = parser
-	| [< '(Binop OpAssign,_); '(Const c,_) >] -> Some c
+	| [< '(Binop OpAssign,_); e = expr >] -> Some e
 	| [< >] -> None
 
 and parse_fun_param_type = parser

+ 7 - 7
typeload.ml

@@ -35,16 +35,16 @@ let type_constant ctx c p =
 	| Ident "null" -> mk (TConst TNull) (ctx.api.tnull (mk_mono())) p
 	| _ -> assert false
 
-let type_function_param ctx t c opt p =
-	match c with
+let type_function_param ctx t e opt p =
+	match e with
 	| None ->
 		if opt then ctx.api.tnull t, Some TNull else t, None
-	| Some c ->
-		let c = (try type_constant ctx c p with _ -> error "Parameter default value should be constant" p) in
-		unify ctx t c.etype p;
-		match c.eexpr with
+	| Some e ->
+		let e = type_expr ctx e true in
+		unify ctx t e.etype p;
+		match e.eexpr with
 		| TConst c -> t, Some c
-		| _ -> assert false
+		| _ -> error "Parameter default value should be constant" p
 
 let exc_protect f =
 	let rec r = ref (fun() ->