Version française
Home     About     Download     Resources     Contact us    
Browse thread
coercions issues
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris King <colanderman@g...>
Subject: Re: [Caml-list] coercions issues
On 3/12/07, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> This expression cannot be coerced to type
>  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m;
> it has type O.o Ast_js.T.expr Mon.m but is here used with type
>  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m
> Type
>  O.o Ast_js.T.expr =
>    [ `Binop of To.binop * To.expr * To.expr
>    | `Cst of To.constant
>    | `Ecall of To.ident * To.arg list
>    | `Fun of To.ident list * To.bloc
>    | `Lval of To.lvalue
>    | `Unop of To.unop * To.expr ]
> is not compatible with type
>  O.o Ast.T.expr =
>    [ `Binop of To.binop * To.expr * To.expr
>    | `Cst of To.constant
>    | `Ecall of To.ident * To.arg list
>    | `Epos of Ast.location * To.expr
>    | `Fun of To.ident list * To.bloc
>    | `Lval of To.lvalue
>    | `Unop of To.unop * To.expr ]
> The first variant type does not allow tag(s) `Epos

What sort of type is 'a O.m (or 'a Mon.m)?  If it's something whose 'a
member is mutable, then such a coercion will be impossible (it would
allow you to set the member to have the `Epos tag, which it can't).
This would also happen if 'a O.m is declared as an abstract type,
since O'Caml assumes invariance by default.  In the latter case, you
need to specify that 'a O.m is covariant with its parameter:

type +'a O.m

This tells Caml that if 'a is a subtype of 'b, then 'a O.m is indeed a
subtype of 'b O.m and you will be allowed to perform the above
coercion.

Hope this helps.

- Chris