# Initialization with \`yield\` doesn't work

**URL:** <https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298>\
**Category:** Help & Support\
**Created:** [January 16, 2019, 1:14pm UTC](https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298 "2019-01-16T13:14:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladfaust](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vladfaust/32/48_2.png) [@vladfaust](https://forum.crystal-lang.org/u/vladfaust)\
**Post date:** [January 16, 2019, 1:14pm UTC](https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298/1 "2019-01-16T13:14:55Z")

</div>

Could you please explain me why this code doesn’t work and if it is by design?

```crystal
module Foo
  def initialize(foo : String, &block : String ->)
    yield foo
  end
end

class Bar
  include Foo

  @bar : String

  def initialize(foo : String)
    super do |foo|
      @bar = foo
    end
  end
end

Bar.new("baz")

```

```console
Error in test_initialize_yield.cr:19: instantiating 'Bar.class#new(String)'

Bar.new("baz")
    ^~~

instance variable '@bar' of Bar must be String, not Nil

Error: instance variable '@bar' was used before it was initialized in one of the 'initialize' methods, rendering it nilable

```

Quote from Crystal Docs ([Blocks and Procs - Crystal](https://crystal-lang.org/reference/syntax_and_semantics/blocks_and_procs.html)):

> When using blocks with `yield` , the blocks are **always** inlined: no closures, calls or function pointers are involved. This means that this:

```auto
def twice
  yield 1
  yield 2
end

twice do |i|
  puts "Got: #{i}"
end

```

> is exactly the same as writing this:

```auto
i = 1
puts "Got: #{i}"
i = 2
puts "Got: #{i}"

```

---

<div class="post-metadata">

**Author:** ![vladfaust](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vladfaust/32/48_2.png) [@vladfaust](https://forum.crystal-lang.org/u/vladfaust)\
**Post date:** [January 16, 2019, 1:22pm UTC](https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298/2 "2019-01-16T13:22:48Z")

</div>

Workaround is doing `@bar : String | Nil`, but it’s still not clear why it doesn’t work if blocks are inlined 🤔

---

<div class="post-metadata">

**Author:** ![RX14](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rx14/32/633_2.png) [@RX14](https://forum.crystal-lang.org/u/RX14)\
**Post date:** [January 16, 2019, 1:26pm UTC](https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298/3 "2019-01-16T13:26:06Z")

</div>

It’s exactly the same performance-wise, not in semantics. The docs are a little misleading, but the section you quoted is clearly under the “performance” heading.

---

<div class="post-metadata">

**Author:** ![vladfaust](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vladfaust/32/48_2.png) [@vladfaust](https://forum.crystal-lang.org/u/vladfaust)\
**Post date:** [January 16, 2019, 1:28pm UTC](https://forum.crystal-lang.org/t/initialization-with-yield-doesnt-work/298/4 "2019-01-16T13:28:24Z")

</div>

I see, thanks for the response. Looks like the compiler isn’t sure for 100% if there will be a `yield` in the `super`.
