* question about polymorphic variants
@ 2008-05-02 17:35 Jacques Le Normand
2008-05-02 18:03 ` Rémi Vanicat
0 siblings, 1 reply; 3+ messages in thread
From: Jacques Le Normand @ 2008-05-02 17:35 UTC (permalink / raw)
To: caml-list caml-list
[-- Attachment #1: Type: text/plain, Size: 798 bytes --]
Hello caml-list,
when I try to run the following code:
type sometype =
([
`Foo of int
| `Bar of int
| `Var of 'a ref
] as 'a)
type someother =
([
`Foo of int
| `Var of 'a ref
] as 'a)
let foo : someother = `Foo 5;;
(foo :> sometype)
in my ocaml repl, it gives me the following error
This expression cannot be coerced to type
sometype = [ `Bar of int | `Foo of int | `Var of sometype ref ];
it has type someother = [ `Foo of int | `Var of someother ref ]
but is here used with type
[< sometype ] = [< `Foo of int | `Var of sometype ref ]
Type someother = [ `Foo of int | `Var of someother ref ]
is not compatible with type
sometype = [ `Bar of int | `Foo of int | `Var of sometype ref ]
The first variant type does not allow tag(s) `Bar
how do I get around this?
[-- Attachment #2: Type: text/html, Size: 1014 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: question about polymorphic variants
2008-05-02 17:35 question about polymorphic variants Jacques Le Normand
@ 2008-05-02 18:03 ` Rémi Vanicat
0 siblings, 0 replies; 3+ messages in thread
From: Rémi Vanicat @ 2008-05-02 18:03 UTC (permalink / raw)
To: caml-list
"Jacques Le Normand" <rathereasy@gmail.com> writes:
> Hello caml-list,
> when I try to run the following code:
>
>
> type sometype =
> ([
> `Foo of int
> | `Bar of int
> | `Var of 'a ref
> ] as 'a)
> type someother =
> ([
> `Foo of int
> | `Var of 'a ref
> ] as 'a)
>
> let foo : someother = `Foo 5;;
> (foo :> sometype)
>
[...]
>
> how do I get around this?
this can't be done: ref type is neither co-variant or
contra-variant :
Imagine:
let foo : someother = `Var (ref Foo 10);;
let bar = (foo :> sometype);;
match bar with
| Var of r -> r:=`Bar 5;;
let baz = match foo with
| Var of r -> x
then baz will be of type someother, with the `Bar 5 value : this can't
be. So you cannot coerce a ref, and so you cannot coerce any value of
a type that include a ref.
--
Rémi Vanicat
^ permalink raw reply [flat|nested] 3+ messages in thread
* Question about polymorphic variants
@ 2005-10-28 9:57 Xavier Clerc
0 siblings, 0 replies; 3+ messages in thread
From: Xavier Clerc @ 2005-10-28 9:57 UTC (permalink / raw)
To: caml-list
Hello,
I have a question concerning polymorphic variants and type inference.
Formulating my question from the example of section 4.2 in the ocaml
manual (version 3.08), I define a function f:
let f = function
| `On -> 1
| `Off -> 0
which is inferred as: val f : [< `Off | `On ] -> int
Then, I use this function in the following expression :
List.map f
which is in turn inferred as: _[< `Off | `On ] list -> int list
My question is about the meaning of the leading underscore in the
inferred type (given that I understand the meaning of the underscore
in an expression such as "Stack.create ()" that is inferred as: '_a
Stack.t).
Regards,
Xavier Clerc
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-05-02 18:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-02 17:35 question about polymorphic variants Jacques Le Normand
2008-05-02 18:03 ` Rémi Vanicat
-- strict thread matches above, loose matches on Subject: below --
2005-10-28 9:57 Question " Xavier Clerc
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox