Re: Prevent accidental object copies

2018-01-14 Thread mratsim
I've raised a similar feature request here: [https://github.com/nim-lang/Nim/issues/6348](https://github.com/nim-lang/Nim/issues/6348) I would love to have different assignment semantics between let or var initialization and already initialized var.

Re: Prevent accidental object copies

2018-01-12 Thread Udiknedormin
@Serenitor It's different in many ways: * also changes semantics of == ("is it actually the same object" instead of "is this (other) object the same") * no dynamic dispatch is usable anyway unless {.inheritable.} is applied first * poorer performance due to dynamic heap allocation (and GC)

Re: Prevent accidental object copies

2018-01-11 Thread Serenitor
This is probably not applicable to what you are trying to do, but maybe you want to use a reference instead? type Person = ref object first, last: string let person1 = Person(first: "John", last: "Doe") # calls `new` implicitly for ref types echo person1.first, "

Re: Prevent accidental object copies

2018-01-10 Thread Udiknedormin
It is. The tricks I showed are for just until the introduction of =sink .

Re: Prevent accidental object copies

2018-01-10 Thread Araq
I think this is implicitly covered by [https://github.com/nim-lang/Nim/wiki/Destructors](https://github.com/nim-lang/Nim/wiki/Destructors) where it would produce a `=sink` or a raw copy.

Re: Prevent accidental object copies

2018-01-01 Thread Udiknedormin
You're welcome. I mostly use Nim for metaprogramming fun so I'm kind of used to these kind of tricks.

Re: Prevent accidental object copies

2017-12-30 Thread cdome
Udiknedormin, nice trick thanks for sharing

Re: Prevent accidental object copies

2017-12-30 Thread Udiknedormin
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

Re: Prevent accidental object copies

2017-12-30 Thread sendell
if you're ok with a var object, you can do: type Person = object first, last: string proc init(self: var Person, first, last: string) = self.first = first self.last = last proc `=`*[T](d: var Person; src: Person) {.error.} = discard va

Prevent accidental object copies

2017-12-29 Thread Nycto
I've got an object where I would like the compiler to fail if I accidentally try to copy an instance of it. Overloading proc = seems to be the way to do this, but I'm struggling to get the exact incantation to also allow for instantiation. Here is my starting point: type Person = o