Workaround for {{ @type.class_vars }} out of method scope


From what I understand, the {{@type.class_vars}} macro expression works only in the scope of a method.

Is there a workaround to obtain the same results, but within the @type scope?

I’d like to automatically collect all the class variables of a module to define their type instead of doing it manually.

For example:

class C; end

# manual way
module M
  @@a : C
  @@b : C

  # ... other code using the variables

# automatic way
module M
  # currently doesn't work, but something similar would be nice
  {for name in}
    {{}} : C
  {% end %}

  # ... other code using the variables

No, it only works in the context of a method. Even if it did I’m not sure what you’e expecting to happen there as the module has no class vars anyway. You couldn’t use a macro to iterate over things that don’t yet in order to define them.

You could maybe look into how the record macro works and use that to do something like:

# Define the module, accept a variadic amount of variable names 
# that you could then iterate over to create the class vars in the module.
make_module M, a, b, c do
  # Other code using the class vars