【发布时间】:2021-08-14 04:13:31
【问题描述】:
为什么这个字符串到枚举转换器宏无法编译? play
import std/macros, std/strutils
macro autoconvert(TT: type[enum]) =
let fname = "to" & $(TT)
quote do:
converter `fname`*(s: string): `TT` = parse_enum[`TT`](s)
type Tag* = enum movies, books
autoconvert Tag
错误
play.nim(12, 13) template/generic instantiation of `autoconvert` from here
play.nim(7, 3) Error: identifier expected, but found '"toTag"'
更新:
解决方案是使用ident "to" & $(TT),但为什么它不适用于字符串或newLit?
【问题讨论】:
-
fname需要是一个 NimNode(正确的类型),因为宏返回 AST,而不是字符串,并且因为quote在反引号内替换了 NimNode。
标签: nim-lang