Thank you @leorize and @mashingan! It's basically like when you define a 
variable normally; it's a statement, not an expression so nothing is returned. 
Eg: in nim secret, if you type 5 you get 5 back, but if you type let x = 5, you 
get nothing back.

A question that I have is how do you make a template return a lambda (without 
using a macro like =>). Can you? I did some experimenting here: 
[https://play.nim-lang.org/#ix=26E8](https://play.nim-lang.org/#ix=26E8) (turn 
on debug to see the macro treeRepr output).

Code reproduced below: 
    
    
    import algorithm, macros, sugar
    
    type
      Foo = object
        x:int
    
    
    var s = @[Foo(x:5),Foo(x:1),Foo(x:2)]
    
    echo s
    
    macro doit1(field:untyped):untyped =
      result = newStmtList(quote do:
        s.sorted(proc (f1, f2: Foo): int = cmp(f1.`field`,f2.`field`)))
      echo "doit1"
      echo result.treeRepr
    
    macro doit2(field:untyped):untyped =
      result = newStmtList(quote do:
        proc myCmp(f1,f2:Foo):int = cmp(f1.`field`,f2.`field`)
        s.sorted(myCmp))
      echo "doit2"
      echo result.treeRepr
    
    macro doit3(field:untyped):untyped =
      result = newStmtList(quote do:
        let myCmp = proc (f1,f2:Foo):int = cmp(f1.`field`,f2.`field`)
        s.sorted(myCmp))
      echo "doit3"
      echo result.treeRepr
    
    macro doit4(field:untyped):untyped =
      result = newStmtList(quote do:
        s.sorted((f1,f2:Foo) => cmp(f1.`field`,f2.`field`)))
      echo "doit4"
      echo result.treeRepr
    
    echo doit1(x)
    echo doit2(x)
    echo doit3(x)
    echo doit4(x)
    
    
    Run

In doit1 and doit3, the comparator is a Lambda, and in doit2 it is a ProcDef 
(which is what I believe it was in @sschwarzer's template). In doit4 the 
comparator is a funny infix macro, which if you look at 
[https://github.com/nim-lang/Nim/blob/version-1-0/lib/pure/sugar.nim#L105](https://github.com/nim-lang/Nim/blob/version-1-0/lib/pure/sugar.nim#L105)
 you'll see is assigned a nodekind of Lambda as well. Can we mark the output of 
a template as a lambda (without going to macros)?

Reply via email to