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

macros.expectKind with errorMessage #18573

Closed
wants to merge 36 commits into from
Closed

macros.expectKind with errorMessage #18573

wants to merge 36 commits into from

Conversation

juancarlospaco
Copy link
Collaborator

juancarlospaco and others added 30 commits September 2, 2020 17:31
@timotheecour
Copy link
Member

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)

@juancarlospaco juancarlospaco deleted the expectKind-no-hardcoded-error branch July 25, 2021 14:59
@juancarlospaco
Copy link
Collaborator Author

@timotheecour
I agree, but then if it has to stay because reasons,
then at least make it barely useful with a non-hardcoded message... 🤷

@timotheecour
Copy link
Member

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)

@juancarlospaco
Copy link
Collaborator Author

juancarlospaco commented Jul 25, 2021

Maybe we can {.deprecated.} ?. 🤔

This is by definition a precondition, it grows bad APIs like these for the lack of a proper DBC module in stdlib.

@Araq
Copy link
Member

Araq commented Jul 26, 2021

So which other languages have a "proper DBC" module in their stdlibs? And how does it help them?

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

Successfully merging this pull request may close these issues.

3 participants