问题描述
我正在尝试通过以下方式在Nim中实现“静态”功能:
# File: OtherObject.nim
type OtherObject* = ref object of RootObj
proc mystatic*(_: typedesc[OtherObject]) = echo "Hi"
# File: test.nim
import OtherObject
OtherObject.mystatic()
Error: type mismatch: got <>
but expected one of:
proc mystatic(_: typedesc[OtherObject])
但是如果我将OtherObject.nim
重命名为otherObject.nim
并不会失败...文件名以大写字母开头的意义是什么? (在Windows上为Nim 1.4.0上。)
解决方法
这很可能与模块和类型符号之间的冲突有关,与文件的大小写无关。
例如这个例子:
file.nimm
type file* = object
proc mystatic*(_: typedesc[file]) = discard
test.nim
import file
file.mystatic()
# Error: type mismatch: got <>
# but expected one of:
# proc mystatic(_: typedesc[file])
# expression: file.mystatic()
mystatic(file)
# Error: type mismatch: got <void>
# but expected one of:
# proc mystatic(_: typedesc[file])
# first type mismatch at position: 1
# required type for _: type file
# but expression 'file' is of type: void
# expression: mystatic(file)
给出相同的错误。如果不能仅通过名称就明确地解析符号,则其类型为void
。重命名后,名称冲突就消失了,一切正常。
Ps:例如,在nim中没有static
具有与C++
相同的含义。没有只能从特定对象访问的全局状态。要执行类似的操作,您可以在模块中使用全局变量,而无需导出(或导出)。