0

I'm trying to implement a "static" function in Nim by doing this:

# File: OtherObject.nim
type OtherObject* = ref object of RootObj

proc mystatic*(_: typedesc[OtherObject]) = echo "Hi"
# File: test.nim
import OtherObject

OtherObject.mystatic()

But it fails with this error:

Error: type mismatch: got <>
but expected one of:
proc mystatic(_: typedesc[OtherObject])

But it doesn't fail if I rename OtherObject.nim to otherObject.nim ... what is the significance of file names starting with a capital letter? (This is on Nim 1.4.0 on Windows).

jjv360
  • 4,120
  • 3
  • 23
  • 37

1 Answers1

1

This is most likely related to clash between module and type symbols and not upper/lower case for file.

E.g. this example:

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)

Gives the same error. If symbols cannot be unambiguously resolved from the name alone it has type void. As soon as you rename things the name clash disappears and everything works.

Ps: there is no static functions in nim in the same sense as C++ for example. There is no global state that can be only accessed from the particular object. To do something like this you can just use the global variable in module without exporting it (or export if you want).

haxscramper
  • 775
  • 7
  • 17
  • Ah that makes sense, thank you. I guess it's just not possible currently to use `typedesc[T]` and have the file be the same name as the type... – jjv360 Oct 25 '20 at 10:14