Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macro args have different types in MacroExpand #115

Open
guoshimin opened this issue Sep 27, 2021 · 1 comment
Open

macro args have different types in MacroExpand #115

guoshimin opened this issue Sep 27, 2021 · 1 comment
Labels

Comments

@guoshimin
Copy link

Given the following basic macro definition

: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?

@cosmos72
Copy link
Owner

cosmos72 commented Jan 9, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants