Updated NEP 2 Catching up with C and Rust: Ownership, destructors, unique pointers (markdown)
This commit is contained in:
parent
2fc3929969
commit
a869097207
|
@ -65,6 +65,36 @@ Move semantics mere introduced into c++ as a new way to construct objects. A mov
|
|||
|
||||
Nim also needs some sort of move semantics, when it doesn't want to have everything garbage collected, but I do think that the c++ way of doing this is just too complicated, and for most cases unnecessary. The most important context for move optimization is the return value, because nobody wants create a complex copy operation that keeps the source object alive, just to delete this objects instantly after that. And to have move optimizations for return values, the language does not need to have additional move operations.
|
||||
|
||||
My proposition is, because Nim does not have uninitialized values, everything that can be expressed with move assignments, can also be expressed with `swap`, and swap already exists. Additionally to this, the compiler could make an optimization that prevents destructor calls on objects that are statically known to have only `zero` memory.
|
||||
# My Suggestion to the problem
|
||||
|
||||
My proposition is, to use swap, for things that are solved with move in other languages (C++). The idea to do is, comes from the fact, that Nim does not have the concept of a contructor, and therefore is not able to introduce a new kind of contructor. Neither does it have a state to describe a variable is moved from (ivalid to read).
|
||||
|
||||
Swap has the advantage, that it never introduces new entities, it just swaps two objects of the same type.
|
||||
|
||||
|
||||
## example
|
||||
|
||||
without swap:
|
||||
```Nim
|
||||
var a: MyObjectType
|
||||
var b: MyMemberType
|
||||
b.initialize
|
||||
a.b = b
|
||||
```
|
||||
|
||||
In this case the local variable `b` needs to be copied into `a`, while leaving the local b intact. Everything that is still done to the local `b` is probably it's destruction. And the assignment operator So there is an unnecessary duplication of
|
||||
|
||||
after:
|
||||
```Nim
|
||||
var a: MyObjectType
|
||||
var b: MyMemberType
|
||||
b.initialize
|
||||
swap(a.b,b)
|
||||
```
|
||||
|
||||
|
||||
My proposition is, because Nim does not have uninitialized values. everything that can be expressed with move assignments, can also be expressed with `swap`, and swap already exists. Additionally to this, the compiler could make an optimization that prevents destructor calls on objects that are statically known to have only `zero` memory.
|
||||
|
||||
## swap does already exist, what does this proposition solve at all?
|
||||
|
||||
Then the language would not need new features, it would just need to improve on already existing features.
|
||||
|
|
Loading…
Reference in New Issue