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

mixin + block expression: works in Generics, not in "normal" proc #7385

Closed
mratsim opened this issue Mar 21, 2018 · 4 comments · Fixed by #24119
Closed

mixin + block expression: works in Generics, not in "normal" proc #7385

mratsim opened this issue Mar 21, 2018 · 4 comments · Fixed by #24119

Comments

@mratsim
Copy link
Collaborator

mratsim commented Mar 21, 2018

While trying to find an alternative to be able to slice with _ like a[1, _, 0] without having to define my own _ I stumbled upon the following:

History:

So I tried to inject mixin _ in a block expression before the identifier resolution happen.
Unfortunately this works in generic procs but not in normal proc.

Test case:

import macros

type CustomSeq*[T] = object
  data*: seq[T]

macro `[]`*[T](s: CustomSeq[T], args: varargs[untyped]): untyped =
  ## The end goal is to replace the joker "_" by something else
  result = newIntLitNode(10)


proc foo1(): CustomSeq[int] =
  result.data.newSeq(10)
  # Doesn't work
  echo ((block:
            # mixin _ # invalid expression
            result[_]
    ))

echo foo1()

proc foo2[T](): CustomSeq[T] =
  result.data.newSeq(10)
  # works fine with generic return type
  echo ((block:
            mixin _ # Works
            result[_]
    ))

echo foo2[int]()

cc @krux02, @GULPF

@saem
Copy link
Contributor

saem commented Mar 24, 2021

Tested in playground, no longer seems to be an issue.

@ringabout
Copy link
Member

It still doesn't work when uncomment mixin _ on Nim 1.4.4, gives Error: invalid expression: mixin _

import std/macros

type CustomSeq*[T] = object
  data*: seq[T]

macro `[]`*[T](s: CustomSeq[T], args: varargs[untyped]): untyped =
  ## The end goal is to replace the joker "_" by something else
  result = newIntLitNode(10)


proc foo1(): CustomSeq[int] =
  result.data.newSeq(10)
  # Doesn't work
  echo ((block:
            mixin _ # invalid expression
            result[_]
    ))

echo foo1()

@metagn
Copy link
Collaborator

metagn commented Aug 23, 2023

You're not supposed to be able to use mixin in non-generic procs, no?

@Graveflo
Copy link
Contributor

It still doesn't work when uncomment mixin _ on Nim 1.4.4, gives Error: invalid expression: mixin _

import std/macros

type CustomSeq*[T] = object
  data*: seq[T]

macro `[]`*[T](s: CustomSeq[T], args: varargs[untyped]): untyped =
  ## The end goal is to replace the joker "_" by something else
  result = newIntLitNode(10)


proc foo1(): CustomSeq[int] =
  result.data.newSeq(10)
  # Doesn't work
  echo ((block:
            mixin _ # invalid expression
            result[_]
    ))

echo foo1()

this appears to be fixed in devel but it needs a test case

Araq pushed a commit that referenced this issue Aug 30, 2024
closes #1969, closes #7547, closes #7737, closes #11838, closes #12283,
closes #12714, closes #12720, closes #14053, closes #16118, closes
#19670, closes #22645

I was going to wait on these but regression tests even for recent PRs
are turning out to be important in wide reaching PRs like #24010.

The other issues with the working label felt either finnicky (#7385,
#9156, #12732, #15247), excessive to test (#12405, #12424, #17527), or I
just don't know what fixed them/what the issue was (#16128: the PR link
gives a server error by Github, #12457, #12487).
metagn added a commit to metagn/Nim that referenced this issue Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants