TypeNode#instance_vars return empty array?

code


class Book
  property name : String
  property price : Float32

  def initialize(@name, @price)
  end
end

macro foo(n)
  {% if ["Path"].includes? n.class_name %}
    p {{ n.resolve }}
    p "fields: {{ n.resolve.instance_vars.map &.name }}"
  {% end %}
end

foo Book

output:

Book
"fields: []"

#instance_vars only works in the context of a method. In all other cases it’ll return an empty array, like it does here.

why not raise an compile time error?

Related: `TypeNode#instance_vars` should error when instance vars are not setup · Issue #10662 · crystal-lang/crystal · GitHub and Unions and macro methods - #8 by straight-shoota

2 Likes