# Is this correct?

**URL:** <https://forum.crystal-lang.org/t/is-this-correct/6279>\
**Category:** Help & Support\
**Created:** [January 8, 2024, 11:16am UTC](https://forum.crystal-lang.org/t/is-this-correct/6279 "2024-01-08T11:16:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![femto](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/femto/32/2246_2.png) [@femto](https://forum.crystal-lang.org/u/femto)\
**Post date:** [January 8, 2024, 11:16am UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/1 "2024-01-08T11:16:35Z")

</div>

```auto
def thrice(&block)
  foo(&block)

end
def foo(&)
  puts "Before 1"
  yield 1
  puts "Before 2"
  yield 2
  puts "Before 3"
  yield 3
  puts "After 3"
end

thrice do |i|
  if i == 2
    break
  end
end
puts "end"

```

15 | thrice do |i|  
^-----  
Error: wrong number of block parameters (given 1, expected 0)

while in ruby it can run.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 8, 2024, 2:01pm UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/2 "2024-01-08T14:01:37Z")

</div>

In Crystal you need to declare the type of the block parameter if you want to capture and forward it. If you omit any type restriction, the implicit type is `Proc(Nil)`. Thus it does not accept any block parameters.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 8, 2024, 2:05pm UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/3 "2024-01-08T14:05:24Z")

</div>

This is also called out in the reference book: [Capturing blocks - Crystal](https://crystal-lang.org/reference/1.10/syntax_and_semantics/capturing_blocks.html).

---

<div class="post-metadata">

**Author:** ![femto](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/femto/32/2246_2.png) [@femto](https://forum.crystal-lang.org/u/femto)\
**Post date:** [January 9, 2024, 3:24am UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/4 "2024-01-09T03:24:50Z")

</div>

`In Crystal you need to declare the type of the block parameter`  
has to specify it is a mental burden,  
why not like fun def sum(a,b) … and can omit it?

Is it possible to omit it in the future version?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 9, 2024, 3:57am UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/5 "2024-01-09T03:57:46Z")

</div>

You only need to specify if you want to capture the block. Otherwise in most cases it’s not required.

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [January 9, 2024, 12:30pm UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/6 "2024-01-09T12:30:45Z")

</div>

you can do

```auto
def thrice(&)
  foo { |*args| yield(*args) }
end
 
def foo(&)
  puts "Before 1"
  yield 1
  puts "Before 2"
  yield 2
  puts "Before 3"
  yield 3
  puts "After 3"
end
 
thrice do |i|
  if i == 2
    break
  end
end
puts "end"

```

if you don’t need to capture block

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [January 9, 2024, 4:55pm UTC](https://forum.crystal-lang.org/t/is-this-correct/6279/7 "2024-01-09T16:55:33Z")

</div>

I come from Ruby too, i consider specify type for block parameter is really useful, and clearly, it make code readable, this is statically typed language, you have to accept the trade off, not bad, this is good part.

Following is the working code.

```crystal
def thrice(&block : Int32 ->)
  foo(&block)
end

def foo(&)
  puts "Before 1"
  yield 1
  puts "Before 2"
  yield 2
  puts "Before 3"
  yield 3
  puts "After 3"
end

thrice do |i|
  if i == 2
    next
  end
end
puts "end"

```
