Version française
Home     About     Download     Resources     Contact us    
Browse thread
Void type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Re: Void type?


On Sun, 29 Jul 2007, Arnaud Spiwack wrote:

> Well, I don't really know why to use a void type in OCaml as well, thus my 
> answer may be quite abstract. But when I provide a new type, I give a way to 
> build it and a way to use it. In the case of the void type, there is no way 
> to build an element of that type, but there is a way to use one such element 
> : a void element can be used in place of any type.

This is why you can not build an element of void type (without Obj.magic)- 
as there is no value which can be (safely) used as any type.  Using 
Obj.magic to create such void types will simply lead to segfaults and 
mysterious bugs in your code, and thus nullify the main advantages of 
Ocaml.  The main reason I can see to use void types is to introduce 
Java-style nulls into Ocaml.  The lack of Java-style nulls is one of the 
main reasons I like Ocaml.

If you want to have a "nullable" type, use 'a option or the equivalent. 
If you want to have a "no value" type, use unit.  As a general rule, if 
you find yourself fighting the type system, step back, rethink the 
problem, and find a solution that works with the type system and not 
against it- you'll generally end up with better code.

Brian