Skip to content

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

Closed
@mratsim

Description

@mratsim

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions