Formatting Tips For Code With Actions
Last changed: -213.199.128.153

.

Check out the formatting tips on the right for help formatting and making links.

Use the template below:

Summary

People frequently want to write code that contains embedded actions (also called statements, i.e. the code is imperative - in F# these are just expressions that return the type "unit", which is here essentially equivalent to the type "void"). Here are some guidelines for which constructs to use to write that code.

   <statement>;
   <statement>;
   <statement>

("<statement>" is an expression with type "unit" - i.e. "void").

   let f x = 
     <statement>;
     let y = <expression> in 
     let z = <expression> in 
     <statement>;
     <expr>

In terms of cost each "let" binding is just like declaring a local in C# with some initialization code. That is, multiple "let/in" pairs are not expensive, and nor is nesting them.

   if x then (
     <statement>;
     <statement>
   ) else (
     <statement>;
     <statement>
   )


   if x then begin 
     <statement>;
     <statement>
   end else begin
     <statement>;
     <statement>
   end
  begin match x with 
  | A -> 
     <statement>;
  | B -> 
     <statement>;
  end;
  <statement>
   let _ = <statement> in 
   let _ = <statement> in
   type C = 
     class
       val id: string
       new(id) = 
            { id = id }
          then 
            printf "the object has been initialized\n"
   do <statement> 
   let _ = <statement>
   let x = 1;;
   x+x;;