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

Table.withValue(): .. else: support #21985

Closed
wants to merge 3 commits into from

Conversation

Menduist
Copy link
Contributor

@Menduist Menduist commented Jun 2, 2023

This PR adds support for

    t.withValue(522, value):
      doAssert false
    else:
      # block is executed when `key` not in `t`
      discard

(previously a do was required)

@metagn
Copy link
Collaborator

metagn commented Jun 2, 2023

Could also do:

import macros

macro unwrapElse(ex: untyped): untyped = # maybe goes somewhere in standard library
  if ex.kind == nnkElse:
    result = ex[0]
  else:
    result = ex
    
template foo(a, b, c) =
  if a:
    b
  else:
    unwrapElse(c)
    
foo false:
  echo "a"
else:
  echo "b"

@Menduist
Copy link
Contributor Author

Menduist commented Jun 5, 2023

Done, though I don't understand the CI failure, seems to be caused by IC trying to use the macro at runtime?

@metagn
Copy link
Collaborator

metagn commented Jun 5, 2023

Probably not worth it to investigate IC, the original way is good enough. Though a way to refactor it might be:

template withValueImpl(..., elseBlock) =
  # original withValue

macro withValue*(..., originalElseBlock) =
  let elseBlock = if originalElseBlock.kind == nnkElse: originalElseBlock[0] else: originalElseBlock
  result = getAst(withValueImpl(..., elseBlock))

so the actual implementation of withValue is a bit more visible.

@Menduist
Copy link
Contributor Author

Menduist commented Jun 5, 2023

The original way also caused the same CI failure, unfortunately

Copy link
Contributor

github-actions bot commented Jun 7, 2024

This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions.

@github-actions github-actions bot added the stale Staled PR/issues; remove the label after fixing them label Jun 7, 2024
Copy link
Contributor

This pull request has been marked as stale and closed due to inactivity after 395 days.

@github-actions github-actions bot closed this Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants