I'm trying to create a macro for generating new classes from string. The simplest thing I started is just generating an empty class with the given name:
macro BuildClassFromString(name)
Implementation.buildClassFromString(ImplicitCTX(), name)
module Implementation
public buildClassFromString(typer: Typer, typeName: PExpr) : PExpr
when (typer.IsMainPass)
match (typeName)
| <[ $(n: string) ]> =>
DefineCTX(typer)
def builder = typer.Env.Define (<[ decl:
partial public class $(n: usesite) {
}
]>)
builder.Compile()
| _ => Message.FatalError ("Type name should be a string literal")
<[]>
BuildClassFromString("My")
def _y = My()
It compiles OK and VS shows no errors, but if I change the usage code in any way (just add a space anywhere), the following error is shown:
"Error: AssertionException has occurred when expanding macro 'BuildClassFromString'"
"Error: the meaning of 'BuildClassFromString' does not allow this operation"
If I rebuild the usage project, the errors disappear until the file is changed again.
So, the question: is it a bug in VS integration or my macro is not perfectly well written (I mean maybe `buildClassFromString` should return `void`)?