Hi Chris,

Robert's described the idiom well. Note that option values are one way of explicitly representing "nothing" - they are not actulaly the same as 'null' values coming through from .NET APIs. You don't tend to use 'null' from F# code much except when interfacing to a .NET API, and indeed F# will complain if you try to use 'null' with F#-defined types.

So in your example you would want (note you drop the 'of' - that's only used in the type declaration for the option type)

1
2
3
4
5
6
7
 

    new(dt:data_object option) = 
        match dt with
         |  Some dt ->    data = dt.Data; 
         | None -> failwith "class Test initialized with None."

Cheers!

Don

By on 6/20/2006 5:43 PM ()

Hi Don,

I tried out the code. It works fine in other parts of the program e.g functions but when I use it in a constructor, I get an error message

This is not a valid construction expression. Constructors can only be implemented by a limited range of expressions and sequence expressions.

Here is the code.

open System

type data_object = class
val Data : string
new(dt) = { Data = dt; }
end


type Test = class
val data : string
// Constructor
new(dt:data_object option) =
match dt with
| Some dt -> data = dt.Data;
| None -> failwith "class Test initialized with None."
end

Does this limit what can be done inside a constructor?

Thanks

Chris

By on 6/21/2006 10:37 AM ()

Try:

type Test = class
val mutable data : string
// Constructor
new(dt:data_object option) as x =
{ data = null }
then
match dt with
| Some dt -> x.data <- dt.Data
| None -> failwith "class Test initialized with None."
end
;;

It's a requirement that you initalise all your fields before doing other things with the constructor.

By on 6/21/2006 2:47 PM ()

Or if you don't want the mutable use:

1
2
3
4
5
6
type data_object = { Data : string }
type Test = class
  val data : string
  // Constructor
  new(dt:data_object option) as x = { data = match dt with|  Some dt -> dt.Data | None -> failwith "class Test initialized with None." }
end

or

1
2
3
4
5
type Test2 = class
  val data : string
  // Constructor
  new(dt:data_object option) as x = { data = (Option.get dt).Data }
end
By on 6/23/2006 10:55 AM ()

To test for null, just use the keyword null in a pattern.

> let is_null x = match x with | null -> true | _ -> false;;

val is_null : obj -> bool

> is_null null;;
val it : bool = true
> is_null ("hello" :> obj);;
val it : bool = false

The some/none pattern is similar but avoids lots of problem since you are forced to pattern match over it, so you can not to accentally forget to check for null and cause a null reference exception. See following simiple example:

> let is_some x = match x with | Some _ -> true | None -> false;;

val is_some : 'a option -> bool

> let t1 = Some "Hello";;

val t1 : string option

> let t2 = None;;

val t2 : 'a option

> is_some t1;;
val it : bool = true
> is_some t2;;
val it : bool = false
>

By on 6/20/2006 3:53 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