Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Null pointer
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Prakash Countcham <countcham_p.nospam@c...>
Subject: [Caml-list] Null pointer

I wonder if it is possible to implement some optional types (and maybe other 
data structures) with a null pointer when compiling in native code.

It is not possible in general, because we have to distinguish None and Some(None).

But in some cases, we know that the argument of Some can't be None.
Typically, that is the case of record types that are boxed thus can't be null.

So why not adding a keyword that make record types optional for the shake of 

   type record1 = {...}
   type optional record2 = {...}

   record2 would be the same as (record1 option), but would be more efficient.

The UNIX time command on a PC linux shows a big improvement (more than 40%) in 
the following example :

type recor = {nb:int; ch:string};;
let null = {nb=0; ch=""};;
let a = Some {nb=1; ch="a"};;
let b = {nb=1; ch="a"};;

for i = 1 to 100000 do
   for j = 1 to 10000 do
      match a with
        | None -> ()
        | Some c -> ignore(1+c.nb)
(* => 6.970u 0.010s 0:06.98 100.0%    0+0k 0+0io 97pf+0w *)

for i = 1 to 100000 do
   for j = 1 to 10000 do
     if not (b == null) then
       ignore(1+ b.nb)
(* => 3.980u 0.000s 0:03.99 99.7%     0+0k 0+0io 97pf+0w *)

Thank you for your answer,

  Prakash Countcham

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: