-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgets-thunk.cr
51 lines (43 loc) · 925 Bytes
/
gets-thunk.cr
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
# Just reports type and value.
def no_capture(n : Int32?)
if n.nil?
puts "Got nil."
else
puts "Got #{n}, static type #{typeof(n)}."
end
->{ puts "This one never captures." }
end
# Creates a proc that, when called, prints a value and its type.
def capture_param(n : Int32?)
if n.nil?
puts "Got nil."
->{ puts "No capture." }
else
puts "Got #{n}, static type #{typeof(n)}."
->{ puts "Captured #{n}, static type #{typeof(n)}." }
end
end
# Same, but manages more specific typing.
def capture_copy(n : Int32?)
if n.nil?
puts "Got nil."
->{ puts "No capture." }
else
puts "Got #{n}, static type #{typeof(n)}."
copy = n
->{ puts "Captured #{copy}, static type #{typeof(copy)}." }
end
end
if ARGV.empty?
puts "You must pass an argument."
exit 1
end
n = ARGV[0].to_i?
f = no_capture(n)
f.call
puts
g = capture_param(n)
g.call
puts
h = capture_copy(n)
h.call