Version française
Home     About     Download     Resources     Contact us    
Browse thread
Converting variants with only constant constructors to integers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Kaustuv Chaudhuri <kaustuv.chaudhuri@i...>
Subject: Re: [Caml-list] Converting variants with only constant constructors to integers
2010/6/7 bluestorm <bluestorm.dylc@gmail.com>:
> 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