Please understand that I come from a C/C++ world (I do have experience with 
other languages), so I am still trying to find out how things are done.

Well, the 'to' proc is, if I understand correctly, the same as my 
'constructObjectfromJson' proc, only named differently. I will change the name 
to 'to' because that seems to be the standard in Nim then.

But in C++, I would do it differently, I would write something like this:
    
    
    class CObject
    {
    public:
      CObject() { /*default construction*/ }
      CObject(CJsonNode *node) {  /*set member variables based on the contents 
of node*/ }
    private:
      int m_val1;
      std::string m_val2;
    };
    
    
    Run

In Nim, I would have expected something like this:
    
    
    type
      Object = object
        val1: int
        val2: string
    
    proc Object(node: JsonNode): Object =
      result = Object(...)
    
    
    Run

I would then be able to create an instance of Object like this:
    
    
    let js = parseFile("status.json")
    let obj = Object(js)
    
    
    Run

Or even something like:
    
    
    proc `=`(node: JsonNode): Object =
      result = Object(...)
    
    let obj: Object = js
    
    
    Run

But this doesn't seem possible? That's no problem, but I am trying to find out 
what is the common way of doing it then.

If I am reading you correctly, something similar does not exist in Nim and I 
simply need to write a function that creates the Object based on the JsonNode 
and simply call it from my code. Something like this then:
    
    
    type
      Object = object
        val1: int
        val2: string
    
    proc to(node: JsonNode): Object =
      result = Object(...)
    
    let js = parseFile("status.json")
    let obj: Object = to(js)
    
    
    Run

Right?

Reply via email to