> Now it becomes a different problem of information hiding. Do I want to have 
> these fields visible by users? Are there problems using import? I suppose 
> that if I import bar.nim in multiple files, I would get types 
> redefinitions... I have to balance the advantages of both solutions.

As you already mentioned you won't get type redefinitions, that is kind of the 
point. Information hiding is of course the other purpose of modules (which is 
what an imported file is called in Nim). Typically you would put your type, and 
any proc that handles that proc in the same module, and then you can import it 
and the user is sure they won't be able to access something that would break 
the module if handled incorrectly. In your example you could for example move 
the `$` procedure into that module so that you could echo that type. Or if you 
want the user to be able to read these fields, but not write them you could do 
`template major*(v: Version): uint = v.major` in your module and then hide the 
`major` field in the `Version` object. This means that you will be able to do 
`echo version.major` but not `version.major = 100`. For example:
    
    
    type V = object
      m: uint
    
    # Since this is a single file we call it mm to avoid collision.
    template mm(v: V): uint = v.m
    
    var x = V(m: 100)
    echo x.m
    echo x.mm
    x.m = 10
    #x.mm = 200 # This doesn't work
    
    
    Run

Reply via email to