You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:import (
"fmt"
"go/ast"
)
:macro myMacro(arg ast.Node) ast.Node {
fmt.Printf("arg has type %T\n", arg)
return ~'{1}
}
When applied directly in the REPL, I got
gomacro> myMacro; foo
arg has type *ast.Ident
{int 1} // untyped.Lit
When used inside MacroExpand, I got
gomacro> MacroExpand(~'{myMacro; foo})
arg has type *ast.ExprStmt
1 // go/ast.Node
true // bool
It makes it hard to write macro code that works both when being applied directly and when inside MacroExpand. Is this an issue or do I have some wrong preconception on how things are supposed to work?
The text was updated successfully, but these errors were encountered:
Usually there is no need to look inside the arguments passed to a macro - you simply insert them into a larger AST fragment.
As long as doing that works, the concrete type of an argument does not matter much.
The difference you observe is because it's not possible to directly insert an *ast.Ident inside an *ast.BlockStmt - it must be wrapped (at least) in an *ast.ExprStmt
Given the following basic macro definition
When applied directly in the REPL, I got
When used inside MacroExpand, I got
It makes it hard to write macro code that works both when being applied directly and when inside MacroExpand. Is this an issue or do I have some wrong preconception on how things are supposed to work?
The text was updated successfully, but these errors were encountered: