caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] using a restricted subset of a polymorphic variant
@ 2015-09-10  9:07 Martin DeMello
  2015-09-10  9:12 ` Gabriel Scherer
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Martin DeMello @ 2015-09-10  9:07 UTC (permalink / raw)
  To: OCaml List

[-- Attachment #1: Type: text/plain, Size: 860 bytes --]

I'm not sure exactly why this doesn't work:

type dir = [`North | `South | `East | `West | `Up | `Down]

let index = function
  | `North -> 1
  | `South -> 2
  | `East -> 3
  | `West -> 4
  | `Up -> 5
  | `Down -> 6

class foo = object(self)
  val mutable heading = `Up

  method flip_heading =
    heading <- match heading with `Up -> `Down | `Down -> `Up

  method get_direction (x : dir) = index x

  method get_heading = self#get_direction heading
end

it fails with
File "test.ml", line 28, characters 42-49:
Error: This expression has type [ `Down | `Up ]
       but an expression was expected of type dir
       The first variant type does not allow tag(s)
       `East, `North, `South, `West

but why is that an error? I'd think that any function that accepts type dir
should accept type [`Down | `Up] as well. Also, how do I get this to work?

martin

[-- Attachment #2: Type: text/html, Size: 2402 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-09-10 19:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-10  9:07 [Caml-list] using a restricted subset of a polymorphic variant Martin DeMello
2015-09-10  9:12 ` Gabriel Scherer
2015-09-10  9:13 ` Edouard Evangelisti
2015-09-10 18:03   ` Martin Jambon
2015-09-10 19:14     ` Martin DeMello
2015-09-10  9:13 ` Christoph Höger
2015-09-10  9:19 ` Jacques Garrigue
2015-09-10  9:22 ` Romain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).