Hi,

It seems that, as Julien mentioned, Not_found is not being thrown correctly. However, one way to avoid that problem (and avoid "expensive" error processing), is to use Array.tryfind. This will return an option, with Some(x) representing the match and None representing no item found.

let CompareProcess (p1:string, p2:string) =
p1.GetHashCode() = p2.GetHashCode()

let (|GetMyProcess|_|) (prname:string) =
System.Diagnostics.Process.GetProcesses()
|> Array.tryfind(fun pr -> CompareProcess(pr.ProcessName, prname))

let ProcessTheProcess = function
| GetMyProcess(pr) -> print_endline pr.ProcessName; print_endline "Wait for debug"
| _ -> print_endline "No such process found"

ProcessTheProcess "MY_PROCESS_ID"

Once you use Array.tryfind, you have some options on processing the output. One is to use partial active patterns, as above. The other is to do a simple match.

Regards,

Z.

By on 10/23/2007 8:56 AM ()

The F# library defines "Not_found" as follows. This should explain the mystery above. We've corrected the documentation of functions that indicate they throw 'Not_found' to indicate that they throw 'Not_found/IndexOutOfRangeException'.

exception Not_found = System.IndexOutOfRangeException

Some people have queried whether this is the 'right' exception to be throwing hwere, i.e. whether Not_found should be mapped to the above .NET exception name. It is possible this choice may be reassessed during the process of polishing the language for 'product status'. As a result I encourage you to use 'Not_found' to catch your exceptions here (i.e. the plan is that the functions that indicate they throw 'Not_found' will always throw 'Not_found' regardless of which .NET excecption this is mapped to)

Kind regards

Don

By on 10/23/2007 9:53 AM ()

From F# doc :

1
 val find : ('a -> bool) -> 'a array -> 'a


return the first element for which the given function returns "true". Raise Not_found if no such element exists.

Inside array.fs it calls not_found() which should return Not_found, but apparently doesn't. As in your example

1
Array.find (fun x -> false) [|0|] 

raises the Out_of_range exception as well.

julien

By on 10/23/2007 4:44 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