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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 46F037F1AA for ; Thu, 10 Sep 2015 20:03:46 +0200 (CEST) IronPort-PHdr: 9a23:NEbfDhV+5vxYaOloY23aZYuYN2XV8LGtZVwlr6E/grcLSJyIuqrYZhyOt8tkgFKBZ4jH8fUM07OQ6PC8HzVeqs/b6jgrS99laVwssY0uhQsuAcqIWwXQDcXBSGgEJvlET0Jv5HqhMEJYS47UblzWpWCuv3ZJQk2sfTR8Kum9IIPOlcP/j7n0oM2PJVwWz2PnO/tbF1afk0b4joEum4xsK6I8mFPig0BjXKBo/15uPk+ZhB3m5829r9ZJ+iVUvO89pYYbCf2pN4xxd7FTDSwnPmYp/4Wr8ECbFUrcrkcbB0I9mx9MSy3M7RX7WJi5lirhqqIp0yCfOYjyTKsocTWk9aZiDhHy3nQpLTk8pUTekNBxiup5qReroBo3l4vdep2UMrx8d6bXetUySGxaG8JAUCoHBZmzOdhcR9EdNPpV+tGu72AFqgGzUEz1XLvi Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=mjambon@gmail.com; spf=Pass smtp.mailfrom=mjambon@gmail.com; spf=None smtp.helo=postmaster@mail-pa0-f53.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of mjambon@gmail.com) identity=pra; client-ip=209.85.220.53; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="mjambon@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of mjambon@gmail.com designates 209.85.220.53 as permitted sender) identity=mailfrom; client-ip=209.85.220.53; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="mjambon@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-pa0-f53.google.com) identity=helo; client-ip=209.85.220.53; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="postmaster@mail-pa0-f53.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BjAQCXxfFVlDXcVdFdgneBAGmDJrwChgGBUTwQAQEBAQEBAQEQAQEBAQcLCwkfMIIdggcBAQQSEQQRCAE3AQEDDAEFBRgCAgUWCAMCAgkDAgECAQ8TAQUBCxEGDQEFAgEBHod3AxIBBKo2gTA+MYtBhGWKECcNhGMBCxoBBQ6BFINBghCEe4JPggoagwmBQwEEjWyHaoUKhgODOUaGWopQg1CCIDWBFzhlgUmCHVGISQEBAQ X-IPAS-Result: A0BjAQCXxfFVlDXcVdFdgneBAGmDJrwChgGBUTwQAQEBAQEBAQEQAQEBAQcLCwkfMIIdggcBAQQSEQQRCAE3AQEDDAEFBRgCAgUWCAMCAgkDAgECAQ8TAQUBCxEGDQEFAgEBHod3AxIBBKo2gTA+MYtBhGWKECcNhGMBCxoBBQ6BFINBghCEe4JPggoagwmBQwEEjWyHaoUKhgODOUaGWopQg1CCIDWBFzhlgUmCHVGISQEBAQ X-IronPort-AV: E=Sophos;i="5.17,506,1437429600"; d="scan'208";a="145397692" Received: from mail-pa0-f53.google.com ([209.85.220.53]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Sep 2015 20:03:29 +0200 Received: by padhy16 with SMTP id hy16so49434523pad.1 for ; Thu, 10 Sep 2015 11:03:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=CM4YVfly2MSMuz/cLf+wlCXlxxc17HxSBkz+oa0kavw=; b=GiUybF3+CmCAwxGRSdBtLmr6NiKifYNTX3FGHKb5PWWjJRr4nKnpya7NR9vSQq7wUJ iItHoZQXi+VxypP5CGZRw4d+ojPGm0d+ANLs3FGArJTKqZ7GNMVJNseeMCsgJR0qdnhq 764fVf6gNvZC35nt98OrwaRKqmbgBHivOj3WVPa3Ypt90a62m8nr3saa5XXiimZFYG8m B8eTUzFRhimSlOrSRPU+WnM/VhGntn8+rfOsr5ytqzq/PmVvmXKYnUDDFB1nz1gouiSA 754uwduim4rBtryxh36wsWuJ80o153c1nv0Uqx1MJ0dZyD4Nsr3PVf2iEmxWEbtseETi S3BQ== X-Received: by 10.68.231.5 with SMTP id tc5mr86653323pbc.54.1441908208129; Thu, 10 Sep 2015 11:03:28 -0700 (PDT) Received: from ?IPv6:2601:646:c201:9a00:60ff:9202:fac7:440e? ([2601:646:c201:9a00:60ff:9202:fac7:440e]) by smtp.googlemail.com with ESMTPSA id b7sm13260245pbu.20.2015.09.10.11.03.27 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 10 Sep 2015 11:03:27 -0700 (PDT) Sender: Martin Jambon Message-ID: <55F1C5EE.7010205@ens-lyon.org> Date: Thu, 10 Sep 2015 11:03:26 -0700 From: Martin Jambon User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: OCaml Mailing List CC: Edouard Evangelisti , Martin DeMello References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant I'd like to add that this a good systematic way to avoid confusing error messages when dealing with polymorphic variants (or objects). Just add type annotations on variables and function parameters which are polymorphic variants (or objects). Here only one extra annotation would be needed, on the heading value: val mutable heading : dir = `Up method flip_heading = ... method get_direction (x : dir) = ... method get_heading = ... The error message becomes closer to the actual error: File "toto.ml", line 15, characters 15-61: Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: (`West|`South|`North|`East) Martin On 09/10/2015 02:13 AM, Edouard Evangelisti wrote: > Dear Martin, > > In your example, heading is not of type dir. > However, this would work : > > val mutable heading : dir = `Up > > Regards, > Edouard > > > 2015-09-10 10:07 GMT+01:00 Martin DeMello >: > > 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 > >