1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
|
// A method with a “normal” (call-by-value) argument is declared like this
(*
def foo(x: Int) = {
val a = x
val b = x
(a, b)
}
foo({println("hello"); 2 + 2})
//hello
//res0: (Int, Int) = (4,4)
*)
let foo (x : int) =
let a = x
let b = x
a, b
foo (printfn "Hello"; 2 + 2)
//Hello
//val it : int * int = (4, 4)
// A method with a lazy (call-by-name) argument is declared like this
(*
def foo(x: => Int) = {
val a = x
val b = x
(a, b)
}
foo({println("hello"); 2 + 2})
//hello
//hello
//res0: (Int, Int) = (4,4)
*)
let foo (x : unit -> int) =
let a = x()
let b = x()
a, b
foo <| fun () -> printfn "hello"; 2 + 2
//hello
//hello
//val it : int * int = (4, 4)
// A method that takes a no-argument function (a thunk) is declared like this
(*
def foo(x:() => Int) = {
val a = x
val b = x
val c = x()
(a, b, c)
}
foo(() => {println("hello"); 2 + 2})
//hello
//res0: (() => Int, () => Int, Int) = (<function>,<function>,4)
*)
let foo (x : unit -> int) =
let a = x
let b = x
let c = x()
a, b, c
foo <| fun () -> printfn "hello"; 2 + 2
//hello
//val it : (unit -> int) * (unit -> int) * int =
// (<fun:it@65-10>, <fun:it@65-10>, 4)
// Lazy values allow you to turn call-by-name lazy arguments into call-by-need lazy arguments.
(*
//def foo(def x: Int) = {
def foo(x: => Int) = {
lazy val a = x
(a, a, a)
}
foo({println("hello"); 2 + 2})
hello
res0: (Int, Int, Int) = (4,4,4)
*)
let foo (x : Lazy<int>) =
let a = x
a.Value, a.Value, a.Value
foo <| lazy (printfn "hello"; 2 + 2)
//hello
//val it : int * int * int = (4, 4, 4)
|