From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id CB9E57F1AA for ; Thu, 10 Sep 2015 11:07:23 +0200 (CEST) IronPort-PHdr: 9a23:CPn88BchyryH96doFUIc6wK8lGMj4u6mDksu8pMizoh2WeGdxc69Yx7h7PlgxGXEQZ/co6odzbGG7+a5BSQp2tWojjMrSNR0TRgLiMEbzUQLIfWuLgnFFsPsdDEwB89YVVVorDmROElRH9viNRWJ+iXhpQAbFhi3DwdpPOO9QteU1JTrkbnus7ToICx2xxOFKYtoKxu3qQiD/uI3uqBFbpgL9x3Sv3FTcP5Xz247bXianhL7+9vitMU7q3cYk7sb+sVBSaT3ebgjBfwdVWx+cjN92Mq+kjLKSA/HwnoHTi1CmRNNB03B7Qrmdoboryb/8Ot7wiiTe8PsG+MaQzOnuoJiUgPpjm8rOhs09GXQwph1hbhHoRStqhlyx4rTSI6QPft6OKjaeIVJFiJ6Qs9NWnkZUcuHZIwVAr9EZL4Aog== Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=martindemello@gmail.com; spf=Pass smtp.mailfrom=martindemello@gmail.com; spf=None smtp.helo=postmaster@mail-vk0-f49.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of martindemello@gmail.com) identity=pra; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="martindemello@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of martindemello@gmail.com designates 209.85.213.49 as permitted sender) identity=mailfrom; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="martindemello@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-vk0-f49.google.com) identity=helo; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="postmaster@mail-vk0-f49.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BLAgB6R/FVmzHVVdFdg0I1aQaDIK4oizGCJ4c7BzwQAQEBAQEBAQEQAQEBAQEGCwsJIS6CHYIfEQQZARseAxIDBgc3AiQBEQEFARYMNYd2AQMSmV6NPoIFgTA+MYtBgWyCeYlzChknDVaEPAEFDoZljHeBQwWNbIdqhQqHcIISlxoSI4EXESeESx4ziEkBAQE X-IPAS-Result: A0BLAgB6R/FVmzHVVdFdg0I1aQaDIK4oizGCJ4c7BzwQAQEBAQEBAQEQAQEBAQEGCwsJIS6CHYIfEQQZARseAxIDBgc3AiQBEQEFARYMNYd2AQMSmV6NPoIFgTA+MYtBgWyCeYlzChknDVaEPAEFDoZljHeBQwWNbIdqhQqHcIISlxoSI4EXESeESx4ziEkBAQE X-IronPort-AV: E=Sophos;i="5.17,503,1437429600"; d="scan'208";a="176811083" Received: from mail-vk0-f49.google.com ([209.85.213.49]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Sep 2015 11:07:23 +0200 Received: by vkaw128 with SMTP id w128so5962096vka.2 for ; Thu, 10 Sep 2015 02:07:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=aHLUutly+qa5WQhjUtQdc5B0p8wG5OzzP70XiTHEzG8=; b=tnFfzpBG4jiVmxCIBGqwdyvE4epQwPzn2B0JgMKsL98Wuj8khm6U99kQZagUmvPszf JlIO9i6WCLNqKXmZvqujF9N0bPxFBa8NPsJZAv2duFLXHiuCEEzGj34yU4S2lCV+ydRD 9D0XVNSntFZj+pvq8jrd9LIlrvd5kSwyLtfYQyPTrc27SLr81PpVOw6INbFOZFJaGZrv vMz04g8qVz7kaYoc9tS4roOU5jUctbt1x51PMLssYUiYX2ckmKTtEBJ/aECWLo5DKiQ4 GEmvNUNRfuOYXQ+76rJoTT+3tuoGNxniZdx8jTQgYdg304qOr5LhSi9+0JUIQeCknakB QTHA== MIME-Version: 1.0 X-Received: by 10.31.151.80 with SMTP id z77mr961221vkd.18.1441876041975; Thu, 10 Sep 2015 02:07:21 -0700 (PDT) Received: by 10.103.51.142 with HTTP; Thu, 10 Sep 2015 02:07:21 -0700 (PDT) Date: Thu, 10 Sep 2015 02:07:21 -0700 Message-ID: From: Martin DeMello To: OCaml List Content-Type: multipart/alternative; boundary=001a1140f36e4acce6051f60edc9 Subject: [Caml-list] using a restricted subset of a polymorphic variant --001a1140f36e4acce6051f60edc9 Content-Type: text/plain; charset=UTF-8 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 --001a1140f36e4acce6051f60edc9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I'm not sure exactly why this doesn't work:

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

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

c= lass foo =3D object(self)
=C2=A0 val mutable heading =3D `Up

= =C2=A0 method flip_heading =3D
=C2=A0 =C2=A0 heading <- match heading with `Up -> `Down | `= Down -> `Up

=C2=A0 method get_direct= ion (x : dir) =3D index x

=C2=A0 method= get_heading =3D self#get_direction heading
end

it fails with
File "test.ml", line 28, characters 42-49:
<= font face=3D"monospace, monospace">Error: This expression has type [ `Down = | `Up ]
=C2=A0 =C2=A0 = =C2=A0 =C2=A0but an expression was expected of type dir
=C2=A0 =C2=A0 =C2=A0 =C2=A0The first vari= ant type does not allow tag(s)
=C2=A0 =C2=A0 =C2=A0 =C2=A0`East, `North, `South, `West

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

martin
=
--001a1140f36e4acce6051f60edc9--