During my learning of nim, I'm trying to implement some kind of code common for Python OOP. I'm spent a lot of time reading articles, book, forum and feels like I'm more or less OK with basics.
But today I meet strange bug (as I initially think), which is explained in 3 listings below...
File ex.nim
type Entity*[T] = ref object of RootObj
props*: T
id*: int
name*: string
proc uuid*[T](entity: Entity[T]): int =
echo "Running base class uuid ..."
entity.id
File ex2.nim
import ex
type UserProps* = object
username*: string
type User* = ref object of Entity[UserProps]
File ex3.nim
import ex2
# import ex
let up = UserProps(username: "Telega")
let u = User(props: up, id:8)
# src/ex3.nim(8, 7) Error: undeclared field: 'uuid' for type ex2.User [declared in src/ex2.nim(7, 6)]
echo u.uuid
If we try to compile ex3.nim it will fail with error undeclared field: 'uuid' for type ex2.User. When code listed in the file ex3.nim was in ex2.nim all works OK. When I move code to the new file, there is an error.
Then I realize, that importing import ex fixes the error. But I was surprised by this.
And only then I realize, that u.uuid is actually uuid(u) and this procedure was not imported.
So, that is not a history about my smartness :)
My questions are:
I'm pretty sure, that trying to reflect what I was doing in Python for many years is not follow nim-way, but that is my learning curve, so any links to good examples or explanations are welcome, coz I saw a lot of examples of a library-, utility-, framework- related code, but almost no examples of boring, enterprise-related stuff.
Thanks.
Your example can be fixed by doing
export ex.uuid
Or, if you want to export everything from that module (export only exports things which are marked by *:
import ex
export ex
See https://nim-lang.org/docs/manual.html#modules-export-statement
@juancarlospaco thanks, I read links you provide before. Maybe I'm skipped, but there were no explanations of such catch.
@Yardanico oh, thanks a lot. I think that is it!