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"

      

+3


source to share


2 answers


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)

      

+7


source


By ignore

returning to your last line, you are returning a function, not a simple value.



The point ignore

is converting objects to ()

. Your last line might just return ()

directly.

+1


source







All Articles