passing functions in nim
Asked Answered
G

1

7

I'm having issues passing math functions (procs) in 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 the error

Error: 'sqrt' cannot be passed to a procvar

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

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

I'm using square root and map as examples, but eventually I'll be passing sqrt (and other math procs) to another proc. Is there a way to do this without writing wrapper functions?

Gaitan answered 7/4, 2015 at 16:18 Comment(0)
B
5

There are plans to make this work by default, by enabling the procvar pragma by default and making a wrapping procvar for C-imported procs: https://github.com/nim-lang/Nim/issues/2172

Boleyn answered 7/4, 2015 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.