Constraints for `@generated` function

You’ve got the gist of it. To expand a little:

  • The generator code (used to build the AST from the argument types) cannot call functions or methods which are defined after the generated function is defined. So you can’t generally call things like trait functions within your generator if they may be overloaded for user defined types. This may eventually be fixed in a future julia version with some sufficiently clever tracking of function dependencies (“backedges”), we will see.
  • The generated code (which is invoked on the argument values) can call any function, but it may not define new types. This means it can’t use closures because closures are lowered to new types internally. I believe this may also eventually be fixed, but for now we need to put up with it.
7 Likes