Pierre Weis wrote:
> So, adding a test to detect this case we can initialize the vector
> properly, without using Obj.magic.
>
> exception Not_yet_initialized of int;;
> exception Already_initialized of int;;
> exception Never_initialized of int;;
>
> let initialize n f =
> if n = 0 then [||] else
> let init_v = Array.make n false in
> let v = ref [||] in
> let get i = if init_v.(i) then !v.(i) else raise (Not_yet_initialized i) in
> let set i ei =
> if !v = [||] then v := Array.make n ei;
Hmmm. This should work, even if 'ei' has a finaliser or mutable
field: 'ei' isn't a 'dummy' value, but a real value that the client
wanted in the array. On the other hand, a dummy value the client
is forced to supply may have dire consequences where the type
is either a class instance , or finalised: here
either construction or destruction may have arbitrary
semantics.
So this (the code you gave) is much better than having to supply a dummy
value.
The same problem occurs with 'forced' variable initialisation:
dummy values may have unwanted side-effects.
There is a tension here, since ocaml is not a purely
functional language.
-- John (Max) Skaller at OTT [Open Telecommications Ltd] mailto:maxs@in.ot.com.au -- at work mailto:skaller@maxtal.com.au -- at home
This archive was generated by hypermail 2b29 : Wed May 24 2000 - 10:11:55 MET DST