List Info

Thread: "ocaml_beginners"::[] using string array option type




"ocaml_beginners"::[] using string array option type
user name
2007-01-05 19:54:11

On Fri, 5 Jan 2007, al_kolesnikoff wrote:

> --- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Martin Jambon
&gt; <martin_jambon...> wrote:
&gt;>
>;> Your dbs object is not exactly an array, it is an option type: its
> value
>;> is either "None" or "Some a" where "a&quot; is an array.
&gt;> If you want to get the length of the array, you should do something
> like:
>;>
>> match dbs with
>> None -> failwith "I cannot answer your question&quot;
>&gt; | Some a -> Array.length a
>
&gt; Does this method only for work with "option type" ?

No, it's a very general and powerful mechanism in OCaml.
These kind of types are called sum types or variants, and the "match with"
stuff is what is called pattern matching. I think it's nicely explained
there:

http://www.ocaml-tutorial.org/data_types_and_matching

Martin

--
Martin Jambon
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )