I'm new to F #, so it's hard for me to change my mindset after many years of C # / Java OOP.
I have an event handler MyForm.SelectFile(filePath:String)that opens a dialog box and allows me to select a file to read. After selecting the file, it is called Parser.LoadFile(filePath:String):
static member LoadFile(filePath:String) =
if not <| ZipFile.IsZipFile(filePath) then
failwith "invalid file specified."
use zipFile = new ZipFile(filePath)
if zipFile.Count <> 2 || zipFile |> Seq.exists(fun x -> x.FileName <> "alpha" && x.FileName <> "beta") then
failwith "invalid file specified."
zipFile |> fun x -> Parser.Parse(x.OpenReader())
I always expect that the selected file will be a valid zip archive containing 2 files without the extension: "alpha" and "beta".
Firstly, is there a better way to misinform my entrance?
My if statements are pretty long, and I'm sure F # can provide better solutions, but I really can't figure it out.
Secondly, using it failwithforces me to handle exceptions in my method MyForm.SelectFile(filePath:String), and I think options might be a better solution.
, , (ZipFile.IsZipFile ), ZipFile.
# null , , null , .
:
type Parser with
static member isValidZipFile (zipFile:ZipFile) =
(zipFile.Count = 2) && (zipFile |> Seq.forall(fun x -> (x.FileName = "alpha") || (x.FileName = "beta")))
static member LoadFile(filePath:String) =
if not <| ZipFile.IsZipFile(filePath) then
None
else
use zipFile = new ZipFile(filePath)
if not <| Parser.isValidZipFile(zipFile) then
None
else
Some(seq { for zipEntry in zipFile do yield Parser.Parse(zipEntry.OpenReader()) } |> Seq.toArray)