How about to create a macro like this and use it?
    
    
    import macros
    
    macro setLambda(procVar: var proc; body: untyped): untyped =
      #echo procVar.getTypeImpl().treeRepr
      let
        typeImpl = procVar.getTypeImpl()
        formalParams = typeImpl[0].copyNimTree
        pragma = typeImpl[1].copyNimTree
        empty = newEmptyNode()
      
      newAssignment(procVar,
                    newTree(nnkLambda,
                            empty, empty, empty,
                            formalParams,
                            pragma,
                            empty,
                            body))
    
    var
      foo: proc (x: int): int
      bar: proc () {.cdecl.}
      baz: proc (a: string; b: float; c: openArray[int]): string {.nimcall.}
    
    # foo = proc (x: int): int = x + 1
    # bar = proc () {.cdecl.} = echo "barrr"
    # baz = proc (a: string; b: float; c: openArray[int]): string {.nimcall.} = 
a & $b & $c
    
    # Following code do samething to above code
    
    setLambda foo, x + 1
    setLambda bar:
      echo "barrr"
    setLambda baz, a & $b & $c
    
    echo foo(10)
    bar()
    echo baz("Test ", 3.14, [123, 456])
    
    Run

Reply via email to