From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id E45E5BC57 for ; Tue, 8 Jun 2010 13:28:45 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.53,384,1272837600"; d="scan'208";a="52665043" Received: from mail-yw0-f172.google.com ([209.85.211.172]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/RC4-MD5; 08 Jun 2010 13:28:45 +0200 Received: by ywh2 with SMTP id 2so1377168ywh.0 for ; Tue, 08 Jun 2010 04:28:44 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.247.211 with SMTP id md19mr5731935qcb.146.1275996524172; Tue, 08 Jun 2010 04:28:44 -0700 (PDT) Received: by 10.229.4.204 with HTTP; Tue, 8 Jun 2010 04:28:44 -0700 (PDT) In-Reply-To: References: <4C0D3557.1020200@gmail.com> <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> <4C0D4C88.4070102@gmail.com> Date: Tue, 8 Jun 2010 13:28:44 +0200 Message-ID: Subject: Re: [Caml-list] Converting variants with only constant constructors to integers From: Kaustuv Chaudhuri To: caml-list caml-list Cc: bluestorm Content-Type: text/plain; charset=UTF-8 X-Spam: no; 0.00; variants:01 constructors:01 integers:01 foo:01 integers:01 foo:01 compiler:01 segfaults:01 ocaml:01 val:01 4.2:98 caml-list:01 constructor:01 int:01 int:01 2010/6/7 bluestorm : > If you were, for example, to change your definition of "foo" (in a way not > always representable as integers, such as adding a constructor with > parameters), int_of_foo would become incorrect. The compiler wouldn't warn > you, and you could get pretty bad failures (segfaults). You're on your own. %identity will never cause a seg fault. % ocaml Objective Caml version 3.12.0+dev25 (2010-05-20) # external intify : 'a -> int = "%identity" ;; external intify : 'a -> int = "%identity" # intify "hello, world" ;; - : int = 70165942419644 # intify [10; 20] ;; - : int = 70165942402044 # intify 4.2 ;; - : int = 70165942392140 # intify 322L ;; - : int = 70165942379652 # intify 322L ;; - : int = 70165942369800 Obviosly if you write code that depends on such values, then your code is most likely broken: # let x = [10; 20] ;; val x : int list = [10; 20] # intify x ;; - : int = 70165942003824 # Gc.compact () ;; - : unit = () # intify x ;; - : int = 70165941843960 -- Kaustuv