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
    
    
    Run

File **ex2.nim**
    
    
    import ex
    
    type UserProps* = object
        username*: string
    
    
    type User* = ref object of Entity[UserProps]
    
    
    Run

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
    
    
    Run

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:

  * What is a nim way to write such code? (I'm thinking about `include`, but 
not sure, this is the true way.)
  * In other words, how should I notify users of my code, that they have to 
import additional modules?
  * How to explain this approach/behavior to Python people? (I'm already 
explained this to myself, but I do not know how to live with this knowledge 
comfortably)



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.

Reply via email to