boo-lang/boo

Macro in class definitions ?

Closed this issue · 1 comments

Consider following code:

class a():
	macro int0(name):
		yield [| $name = 0 |]

	def constructor():
		int0 test
		print test

a()

Surely it looks OK for you, right ? So well, compiler disagrees here:

BCE0005: Unknown identifier: 'int0'

Any way to fix that thing ?

The macro expander does not search for macros nested inside of a class--with the exception of macros nested in other macros, which are only in-scope inside an invocation of the outer macro--and changing that would potentially be very messy, especially in scenarios like this where the macro is defined inside the same project that's using it.

I've updated MacroMacro to catch this scenario and error out, giving you a more clear idea of what's going wrong, so you'll know you have to declare the macro at the top level rather than inside of a type.