GithubHelp home page GithubHelp logo

Comments (1)

realratchet avatar realratchet commented on June 5, 2024

In case there isn't a way to do this currently, how about something like a bang operator.

template toBangArg(inNode: NimNode, plainArgs: NimNode, kwArgs: NimNode) =
  var node: NimNode
  
  if inNode.kind == nnkPar:
    node = inNode[0]
  else:
    node = inNode
  
  if node.kind == nnkHiddenStdConv and node[0].kind == nnkEmpty:
    discard
  elif node.kind == nnkExprEqExpr or node.kind == nnkAsgn or node.kind == nnkExprColonExpr:
    kwArgs.add(newTree(nnkPar,
      newCall("cstring", newLit($node[0])),
      newCall("toPyObjectArgument", node[1])))
  else:
    plainArgs.add(newCall("toPyObjectArgument", node))

macro `!`*(o: PyObject, args: varargs[untyped]): PyObject =
  expectKind(o, nnkSym)

  let plainArgs = newTree(nnkBracket)
  let kwArgs = newTree(nnkBracket)

  expectKind(args, nnkArgList)
  expectLen(args, 1)

  if args[0].kind == nnkTupleConstr:
    for arg in args[0]:
      toBangArg(arg, plainArgs, kwArgs)
  else:
    toBangArg(args[0], plainArgs, kwArgs)

  result = newCall(bindSym"newPyObjectConsumingRef",
    newCall(bindSym"callObjectAux", newDotExpr(o, newIdentNode("rawPyObj")), plainArgs, kwArgs))

This would allow the object to be called like this discard tqdm!(nil, desc: "column", total: 5)
I wanted to make it work like this discard tqdm!(nil, desc="column", total=5) but it seems that nim has issue parsing macros with multiple kwargs if no arg is passed, e.g., discard tqdm!(desc="column", total=5) doesn't compile. However, the colon does align nim's constructor style.

If you think this is completely stupid, no problem, I can always work around this on my own. If you think this is something worth pursuing, I can make a PR.

from nimpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.