In addition to Tomas' answer, you can use a monad, e.g. something very similar to the AttemptBuilder example here

[link:blogs.msdn.com]

on either 'option' or 'Nullable' and then use e.g.

myMonad {
let! a = ...
let! b = ...
return a + b
}

By on 3/18/2009 9:25 AM ()

I was hoping to extend the option type doing something like :

\n

type Microsoft.FSharp.Core.option with

static member (-) (v1,v2) =

match (v1,v2) with

| (Some a,Some b) -> Some( a - b)

| _ -> None

\n

But I get the error "

error FS0191: The declared type parameters for this type extension do not match the declared type parameters on the original type 'option<_>''"

Must be missing something...

Thanks for the help

By on 7/15/2009 8:42 AM ()

As of 1.9.6.16 (I think), you need to make sure your type extensions have the same type parameters.

So you'd need "type option<'a> with...". I don't have a compiler at the moment to verify this, but I think that's the syntax.

However, I don't think this will help your scenario, as, at least a while ago, type extensions were not taken into consideration for solving member constraints.

By on 7/15/2009 12:09 PM ()

Hi,
F# doesn't automatically lift operators for nullable or option types, so the easiest way to start is to write something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 


> let lift op a b =
   match a, b with
   | Some(av), Some(bv) -> Some(op av bv)
   | _, _ -> None;;

val lift : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option

> let (+?) a b = lift (+) a b
  let (-?) a b = lift (-) a b;;

val ( +? ) : int option -> int option -> int option
val ( -? ) : int option -> int option -> int option

> (Some 10) +? (Some 32);;
val it : int option = Some 42
> (Some 10) -? None;;
val it : int option = None
By on 3/18/2009 7:45 AM ()
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