I'm experimenting with a `sorted` proc that takes a `key` argument instead of a 
`cmp` argument (see also 
[https://docs.python.org/3/library/functions.html#sorted](https://docs.python.org/3/library/functions.html#sorted)
 ).

This is my attempt (also [on Nim 
playground](https://play.nim-lang.org/#ix=270C)): 
    
    
    func sorted[T, U](a: openArray[T]; key = proc [T](x: T): U): seq[T] =
      discard
    
    let s = @[1, 2, 3]
    echo sorted(s, key = proc (x: int): string = $x)
    
    
    Run

For this input, I get 
    
    
    Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
    Hint: system [Processing]
    Hint: widestrs [Processing]
    Hint: io [Processing]
    Hint: in [Processing]
    /usercode/in.nim(1, 48) Error: expected: ')', but got: '['
    
    
    Run

which is for the `[T]` after the `key = proc`.

On the other hand, if I leave out the `[T]` for the proc (hoping that the type 
`T` from my `sorted` is applied), 
    
    
    func sorted[T, U](a: openArray[T]; key = proc (x: T): U): seq[T] =
      discard
    
    let s = @[1, 2, 3]
    echo sorted(s, key = proc (x: int): string = $x)
    
    
    Run

I get the compiler message 
    
    
    Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
    Hint: system [Processing]
    Hint: widestrs [Processing]
    Hint: io [Processing]
    Hint: in [Processing]
    /usercode/in.nim(1, 51) Error: undeclared identifier: 'T'
    
    
    Run

So it seems the type `T` from `sorted [T, U]` isn't applied to the `key` proc 
argument.

How can I get `sorted` to work? (once the body of `sorted` is implemented, of 
course ;-) )

Reply via email to