Passing functions in nim

I'm having trouble passing math functions (procs) to Nim (version 0.10.2).

import math var s1 = @[1.1, 1.2, 1.3, 1.4] var s2 = map(s1, math.sqrt) 

I get an error

 Error: 'sqrt' cannot be passed to a procvar 

If I write a wrapper function for sqrt, it works fine.

 proc fxn(x: float): float = math.sqrt(x) var s2 = map(s1, fxn) 

I use the square root and the map as examples, but in the end I will pass sqrt (and other math procs) to another proc. Is there a way to do this without writing wrapper functions?

+5
source share
1 answer

It is planned to make this work by default by turning on the default procvar pragma and making a wrapping procvar for C-imported procs: https://github.com/nim-lang/Nim/issues/2172

+3
source

All Articles