Knocking down the F # compiler
The following snippet illustrates the error I am getting. Although both match branches return the same; I get the error: "This expression was supposed to have a type unit, but there is a type" a → unit "here. I don't know what the compiler wants here ...
open System.IO let FileContent contents = match contents with | "" -> None | c -> Some(c) let WriteSomething (contents:string) = let writer = new StreamWriter("") writer.Write( contents ) |> ignore let DoStuffWithFileContents = let reader = new StreamReader( "" ) let stuff = reader.ReadToEnd() match stuff |> FileContent with | Some(c) -> WriteSomething c |> ignore | None -> ignore // <- error on "ignore"
source to share
An operator is ignore
actually a function that takes one input and returns unit
type (F # equivalent void
). So when you have -> ignore
, you return a function ignore
.
Instead, use ()
to represent a value like unit
:
| Some(c) -> WriteSomething c
|> ignore
| None -> ()
But actually, since it StreamWriter.Write
returns void
, none of these ignore
are needed. You could just write it like:
let WriteSomething (contents:string) =
let writer = new StreamWriter("")
writer.Write(contents)
let DoStuffWithFileContents =
let reader = new StreamReader("")
let stuff = reader.ReadToEnd()
match stuff |> FileContent with
| Some(c) -> WriteSomething c
| None -> ()
Or even better, use Option.iter
:
let WriteSomething (contents:string) =
let writer = new StreamWriter("")
writer.Write(contents)
let DoStuffWithFileContents =
let reader = new StreamReader("")
let stuff = reader.ReadToEnd()
stuff |> FileContent |> Option.iter(WriteSomething)
source to share