You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now an object with an RwLock field can't be properly initialized (myobj.rwLock = createRwLock() fails due to a deleted move constructor). This PR addresses this by introducing an in-place initializer (init(myobj.rwLock)).
@Araq checked out byref in the manual, but am still puzzled how this will work in this case.
I have some structure that is shared by multiple threads, and is effectively passed around via a pointer. The structure shouldn't be moved around for the pointer to remain valid. The lock is part of this structure, and must be created in-place or moved in. It can't be moved in since the move constructor is deleted. How does byref help?
Checked out the discussion in #75. Still don't understand how byref is relevant. I need RwLock to occupy a specific chunk of memory. How will byref help with this?
By the way, perhaps the compiler could be patched so that in constructions like
var x: T
x = fn()
or
obj.x = fn()
the special result variable would reuse the already allocated var x or obj.x instead of making a new allocation on the stack. This would remove the need for an in-place constructor for types with deleted copy and sink.
This could be problematic for stuff like x = fn(x), but for types with copy and sink deleted this doesn't make much sense anyway.
No, instead myobj.rwLock = createRwLock() should compile and treated as a move. The RwLock type needs to be marked with byref for this to work.
I have a following toy example
type MyObj {.byref.} = object
value: int
proc `=copy`(a: var MyObj, b: MyObj) {.error.}
proc `=sink`(a: var MyObj, b: MyObj) {.error.}
proc createMyObj(value: int): MyObj =
result.value = value
var x: MyObj
x = createMyObj(3)
This doesn't compile ('=sink' is not available for type <MyObj>).
Nim Compiler Version 2.2.0 [Linux: amd64]
Compiled at 2024-10-02
Copyright (c) 2006-2024 by Andreas Rumpf
git hash: 78983f1876726a49c69d65629ab433ea1310ece1
active boot switches: -d:release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Right now an object with an
RwLockfield can't be properly initialized (myobj.rwLock = createRwLock()fails due to a deleted move constructor). This PR addresses this by introducing an in-place initializer (init(myobj.rwLock)).