# How to add second constructor to class whose superclass initializes all instance variables in a macro literal in the constructor

**URL:** <https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274>\
**Category:** Help & Support\
**Created:** [January 12, 2019, 9:25pm UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274 "2019-01-12T21:25:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [January 12, 2019, 9:25pm UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/1 "2019-01-12T21:25:39Z")

</div>

I have a superclass like so,

```auto
abstract class Klass
  def initialize
    {% for ivar in @type.instance_variables %}
      @{{ivar.name}} = {{ivar.type.id}}.new
    {% end %}
  end
end

```

and when I subclass it without specifying a constructor, it works as expected.

```auto
class OKSubKlass < Klass
  property prop : Hash(String, String)
end

```

However, if I add any constructor at all, I get an error because the superclass’s constructor overloads are no longer detected.

```auto
class NotOK < Klass
  property prop : Array(Int)
  def initialize(arr)
    @prop = arr
  end
end
# gives this error: 
# wrong number of arguments for 'NotOK.new' (given 0, expected 1)
# Overloads are:
# - NotOK.new(arr)

```

If I define the overload and leave it with just `super`, it still doesn’t compile, saying `@prop` needs to be nillable.

```auto
class StillNotOK < Klass
  property prop : Tuple(String)
  def initialize
    super
  end
  def initialize(val)
    @prop = val
  end
end

```

Is there a workaround or way to write this or have I simply encountered something you can’t do in crystal?

---

<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 13, 2019, 1:27pm UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/2 "2019-01-13T13:27:31Z")

</div>

I think the last example should work. `NotOK` won’t compile because adding one constructor hides all the parent classes overloads.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [January 13, 2019, 6:26pm UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/3 "2019-01-13T18:26:08Z")

</div>

I didn’t have time to take a look at this but it’s probably a bug. There’s a bug related to initializing instance vars with that kind of macro (using instance vars of a type). For now I suggest to avoid that.

---

<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 13, 2019, 7:55pm UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/4 "2019-01-13T19:55:09Z")

</div>

Meta – it would be cool to have custom emojis on this forum. A one with Ary’s avatar on it would be so much appropriate for this kind of messages 😆

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [January 14, 2019, 3:04am UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/5 "2019-01-14T03:04:48Z")

</div>

Thanks @asterite, should I open an issue for this then?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [January 14, 2019, 8:59am UTC](https://forum.crystal-lang.org/t/how-to-add-second-constructor-to-class-whose-superclass-initializes-all-instance-variables-in-a-macro-literal-in-the-constructor/274/6 "2019-01-14T08:59:52Z")

</div>

Yes, please, unless you can find an existing one for this (I didn’t try that yet)
