2 people like it.

Lazy values and indentation

Different ways to write lazy values - using the lazy keyword or the Lazy.Create function. Pick the one you like the most!

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
// Possible incorrect indentation :-(
let foo = lazy
  printfn "hi"
  1 + 2

// Uh, oh, this looks a bit ugly...
let zoo = 
  lazy
    printfn "hi"
    1 + 2

// Longer, but simpler indentation...
let bar = Lazy.Create(fun _ -> 
  printfn "hi"
  1 + 2 )
val foo : Lazy<int>

Full name: Script.foo
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val zoo : Lazy<int>

Full name: Script.zoo
val bar : System.Lazy<int>

Full name: Script.bar
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )

--------------------
type Lazy<'T> = System.Lazy<'T>

Full name: Microsoft.FSharp.Control.Lazy<_>
static member System.Lazy.Create : creator:(unit -> 'T) -> System.Lazy<'T>
Raw view Test code New version

More information

Link:http://fssnip.net/mJ
Posted:10 years ago
Author:Tomas Petricek
Tags: lazy