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.
@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)
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, "
It is. The tricks I showed are for just until the introduction of
=sink
.
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.
You're welcome. I mostly use Nim for metaprogramming fun so I'm kind of used to
these kind of tricks.
Udiknedormin, nice trick thanks for sharing
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
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
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
10 matches
Mail list logo