Well, the whole problem is that = can't be AST-overloaded.  That would be the 
best and most nimish solution. However, I found three other solutions as well, 
one of which I will quote. Sadly, all three require patterns so if you use 
\--patterns:off, the checks will be disabled. Here it comes, the best solution 
I found:
    
    
    type Person = object
      first, last: string
    
    proc newPerson(first, last: string): Person =
      result.first = first
      result.last = last
    
    # Surprisingly, this is needed for patterns to notice p=src as an operator 
call.
    proc `=`*(d: var Person; src: Person) = system.`=`(d, src)
    
    # We assume moving is a copy from call.
    template personEqErr*{`=`(p, i)}(p: Person, i: Person{lit|lvalue|`let`}) =
      {.error: "Person doesn't allow implicit copy".}
    
    let person1: Person = newPerson("John", "Doe")
    let person2: Person = Person(first: "John", last: "Doe")
    #let person3 = person1  # error!
    echo person1.first, " ", person1.last
    echo person2.first, " ", person2.last
    

If you'd like to disable built-in constructors too (i.e. only call 
initialization), then here you go:
    
    
    type Person = object
      first, last: string
    
    proc newPerson(first, last: string): Person =
      result.first = first
      result.last = last
    
    # Surprisingly, this is needed for patterns to notice p=src as an operator 
call.
    proc `=`*(d: var Person; src: Person) = system.`=`(d, src)
    
    # We assume moving is a copy from call.
    template personEqErr*{`=`(p, i)}(p: Person, i: Person{~call}) =
      {.error: "Person doesn't allow implicit copy".}
    
    let person1: Person = newPerson("John", "Doe")
    #let person2: Person = Person(first: "John", last: "Doe")  # error!
    #let person3 = person1  # error!
    echo person1.first, " ", person1.last
    

Reply via email to