1
2
3
4
let t = Array.create 10 (ThingInt 0)


let t = Array.init 10 (fun i -> ThingInt i) 

When possible, avoid Array.zero_create. Getting null values is always dangerous.

Laurent.

By on 7/24/2007 11:54 AM ()

Perfect. A collection full of null values scares the red out of my beard.

DC

By on 7/24/2007 1:11 PM ()

Actually, after playing around, that only allows me to make an array of one particular thing out of the union. If I have the union previously defined, I can't seem to make an array that will hold either kind of Thing. Is that possible?

DC

By on 7/24/2007 1:19 PM ()

OK, here are two examples:

1
2
3
4
5
6
7
let t = Array.create 5 (ThingInt 0);;

val t : Thing array

> t.[0] <- ThingFloat 4.2;;

val it : unit = ()

Since t is a Thing array, you can store either ThingInt or ThingFloat.

1
2
3
4
5
6
7
8
9
10
11
> let init_fct x =

  if x % 2 = 0 then ThingFloat 1.1

  else ThingInt x;;

val init_fct : int -> Thing

> let t = Array.init 5 init_fct;;

val t : Thing array

The initialization function can return any kind of Thing.

Keep in mind the type of t is Thing (and not ThingInt or ThingFloat: they are only type constructors). That means t can be any value of type Thing.

Laurent.

By on 7/24/2007 5:30 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper