jjv360
jjv360

Reputation: 4140

What is the significance of uppercase file names?

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).

Upvotes: 0

Views: 128

Answers (1)

haxscramper
haxscramper

Reputation: 775

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).

Upvotes: 1

Related Questions