How to return a module from expressions in f #

How can I return unit from an expression in f #? For instance:

 let readInt = let str = Console.ReadLine() let (succ, num) = Int32.TryParse(str) match succ with | true -> Some(num) | _ -> None match readInt with | Some(v) -> Console.WriteLine(v) | None -> ignore //i don't want to do anything, // but i don't know how to ignore this brunch of the expression 
+6
source share
3 answers

The value (only possible) of a unit in F # is written as

 () 

So your code will become

 ... | None -> () 
+9
source

Just write () as follows

 match readInt with | Some(v) -> Console.WriteLine(v) | None -> () 
+7
source

Remember the unit of measure () , it is convenient in many situations.

In this case, you can use the iter function from the Optional module :

 Option.iter Console.WriteLine readInt 

It also emphasizes the fact that iter functions (for example, those from the Seq , List and Array modules) will always give you a unit () value.

+5
source

Source: https://habr.com/ru/post/951189/


All Articles