如何在Nimrod中使用函数指针?

时间:2014-05-05 18:53:25

标签: nimrod nim

是否可以在Nimrod中使用函数指针?

我尝试过的是:

type fptr = (proc(int):int)

proc f(x:int): int = 
  result = x+1

var myf : fptr = f

echo myf(0)

但是当我尝试编译时,我得到:

Hint: added path: 'C:\Users\Peter\.babel\pkgs\' [Path]
Hint: used config file 'C:\Program Files (x86)\Nimrod\config\nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: hello3 [Processing]
Error: internal error: GetUniqueType
Traceback (most recent call last)
nimrod.nim               nimrod
nimrod.nim               handleCmdLine
main.nim                 mainCommand
main.nim                 commandCompileToC
modules.nim              compileProject
modules.nim              compileModule
passes.nim               processModule
passes.nim               processTopLevelStmt
cgen.nim                 myProcess
ccgstmts.nim             genStmts
ccgexprs.nim             expr
ccgstmts.nim             genStmts
ccgexprs.nim             expr
ccgstmts.nim             genVarStmt
ccgstmts.nim             genSingleVar
cgen.nim                 assignGlobalVar
ccgtypes.nim             getTypeDesc
ccgtypes.nim             getTypeDescAux
ccgtypes.nim             genProcParams
cgen.nim                 fillLoc
ccgutils.nim             getUniqueType
msgs.nim                 internalError
msgs.nim                 rawMessage
msgs.nim                 rawMessage
msgs.nim                 handleError

1 个答案:

答案 0 :(得分:8)

当然你可以使用指针,唯一的问题是你忘了定义第一个参数的名称,不幸的是这会使编译器崩溃。以下示例有效:

type fptr = (proc(x: int):int)

proc f(x:int): int = 
  result = x+1

var myf : fptr = f

echo myf(0)

请注意,您可以省略myf变量声明中的类型。您还可以省略proc类型定义周围的括号。我已在https://github.com/Araq/Nimrod/issues/1183报告了您向开发人员发现的错误。

相关问题