-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
macros.expectKind with errorMessage #18573
macros.expectKind with errorMessage #18573
Conversation
juancarlospaco
commented
Jul 24, 2021
- macros.expectKind with errorMessage timotheecour/Nim#775 (comment)
…a custom errorMessage instead of the hardcoded one
…a custom errorMessage instead of the hardcoded one
same comment as nim-lang/RFCs#185; i don't see why we should introduce N different APIs when a single once would suffice (either doAssert or something similar to unittest.check) |
@timotheecour |
why not add an optional param instead to those APIs, eg: proc expectKind*(n: NimNode; k: set[NimNodeKind]) =
## Checks that `n` is of kind `k`. If this is not the case,
## compilation aborts with an error message. This is useful for writing
## macros that check the AST that is passed to them.
if n.kind notin k: error("Expected one of " & $k & ", got " & $n.kind, n)
=>
proc expectKind*(n: NimNode; k: set[NimNodeKind], msg = "") =
## Checks that `n` is of kind `k`. If this is not the case,
## compilation aborts with an error message. This is useful for writing
## macros that check the AST that is passed to them.
if n.kind notin k:
var msg2 = "Expected one of " & $k & ", got " & $n.kind
if msg.len > 0: msg2.add "; " & msg
error(msg2, n) |
Maybe we can This is by definition a precondition, it grows bad APIs like these for the lack of a proper DBC module in stdlib. |
So which other languages have a "proper DBC" module in their stdlibs? And how does it help them? |