Skip to content
Permalink
Browse files

crystal: A captured block needs to be typed

  • Loading branch information...
zhangkaizhao committed Aug 27, 2018
1 parent 3b9ef6d commit 5780d4a06619f9f8e8292738763aeb9bea577b99
Showing with 35 additions and 0 deletions.
  1. +35 −0 crystal/block_call.cr
@@ -0,0 +1,35 @@
# The wrong example

# via https://getgood.at/crystal/in-a-day
# Blocks can be used like procs and have a call method:

#def cats(&block)
# block.call "are awesome"
#end

# Resolve it

# https://stackoverflow.com/questions/51999447/call-block-with-call-not-work
# https://github.com/crystal-lang/crystal/issues/6597

def cats(&block : String -> _)
block.call "are awesome"
end

# or:
#def cats(&block : String -> String)
# block.call "are awesome"
#end

result = cats do |msg|
"Cats " + msg + "!"
end

puts result

# Tested on:
# $ crystal --version
# Crystal 0.26.0 (2018-08-13)
#
# LLVM: 6.0.1
# Default target: x86_64-pc-linux-gnu

0 comments on commit 5780d4a

Please sign in to comment.
You can’t perform that action at this time.