Kaitai Struct: calculated instances with a condition
<p>I'm trying to get Kaitai Struct to reverse engineer a binary structure. <code>seq</code> fields work as intended, but <code>instances</code> don't seem to work as I want them to.</p>

<p>My binary format includes a header with a list of constants that I parse as <code>header</code> field with <code>consts</code> array subfield:</p>

<pre><code>types:
  header:
    seq:
      # ...
      - id: consts
        type: u8
        repeat: expr
        repeat-expr: 0x10
</code></pre>

<p>However, when I try to use the following declaration:</p>

<pre><code>instances:
  index_const:
    value: '_root.header.consts[idx - 0x40]'
    if: idx &gt;= 0x40 and idx &lt;= 0x4f
</code></pre>

<p>This one is intended to calculate a value of <code>index_const</code> by looking up into array of <code>header.consts</code> if and only if <code>idx</code> is in range of [0x40..0x4f].</p>

<p>I use Python as my target language and I assume that it should generate a code like:</p>

<pre><code>    @property
    def index_const(self):
        if hasattr(self, '_m_index_const'):
            return self._m_index_const
        if self.idx &gt;= 64 and self.idx &lt;= 79:
            self._m_index_const = self._root.header.consts[(self.idx - 64)];
            return self._m_index_const
</code></pre>

<p>However, what I get is:</p>

<pre><code>    @property
    def index_const(self):
        if hasattr(self, '_m_index_const'):
            return self._m_index_const

        self._m_index_const = self._root.header.consts[(self.idx - 64)];
        return self._m_index_const
</code></pre>

<p>Is it just me, am I missing something obvious, or is it a bug in Kaitai Struct?</p>