Can macros match against constant arguments instead of literals?

后端 未结 2 1688
我寻月下人不归
我寻月下人不归 2020-12-21 22:07

Given the macro matching example, this shows how macros can match an argument.

I\'ve made very minor changes here to use numbers:

macro_rules! foo {
         


        
相关标签:
2条回答
  • 2020-12-21 22:55

    No.

    Macros operate on the Abstract Syntax Tree, so they reason at the syntactic level: they reason about tokens and their spelling.

    For example:

    fn main() {
        let v = 3;
    }
    

    In this case, the AST will look something like:

    fn main
        \_ let-binding v
            \_ literal 3
    

    If you ask a macro whether v is 3, it will look at you funny, and wonder why you would try comparing a variable name and a literal.

    0 讨论(0)
  • 2020-12-21 23:01

    I'm fairly sure the answer is "no"; at macro expansion time all you have are token trees - expansion happens before evaluation, or even type inference/checking.

    0 讨论(0)
提交回复
热议问题