MVS idiomatic reference replacements #736
Replies: 0 comments 6 replies
-
I'm not sure I understood your use case, but here's how I pictured it. type A {
public memberwise init
public var mother: {me: B, sister: Int}
public var uncle: Int
public fun bar() inout {
&mother.me.foo(&uncle)
}
}
type B {
public init() { &self.x = 1 }
public var x: Int
public fun foo(inout uncle: Int) inout {
&uncle += x
&x = uncle.copy()
}
}
public fun main() {
var grandmother = A(mother: (me: B(), sister: 0), uncle: 1)
&grandmother.bar()
} Generally, if you shape all your data structure like trees, you can always mutate anything you like from a root using For concurrent access across threads you'll need run-time support. Val requires access to be known unique statically but it also offers an escape hatch to express safe APIs using unsafe constructs guarded by the necessary run-time checks. That said, concurrency is a tricky domain. Once you lose local reasoning it becomes difficult to make sense of your program, even if you have safety. Value semantics will keep you out of data races but won't address other race conditions. My personal opinion is that threads should probably be banned from the user model in general, just like pointers and references. There are other ways to do concurrency. |
Beta Was this translation helpful? Give feedback.
-
It would be good to have a list of how to replace references and pointers in situations, when those are used in C++.
One example where a solution was provided is the graph by replacing pointers with lists of indices together with the parent object.
Another example are iterators, where there are also solutions with indices or subscripts.
How would one do it for situations, which currently rely on ownership semantics. We are in a member function (method). The struct we need to write-access is stored in an outer struct (which also contains the object, for which the member function is called, so I am talking about an uncle-nephew relationship of the parts) as separate part. We know that that outer struct is alive as it outlives us (us=the struct the member function is called for). In C++ probably our constructor would receive a (non-owning) reference to that outer struct and we would store it as member variable.
Perhaps for those kind of hierarchical ownership guarantees, there would have to be a separate mechanism. Like: An object always has full access to all its encompassing objects? But in that generality that would open up possible aliases.
Or member functions would (in those cases) need the outer struct as separate parameter. That would feel a lot like global variables and those outer structs can be used (mutatingly) only once at the same time, so one would have to specify the actual sub-object (the uncle from the grandmother). Would each member function caller have to do this for each call?
——————
Another use case is a struct, which several threads want to access from time to time. We want to block, if it is currently not available. So we would need a handle and a function to call returning the object and another function for releasing it?
There are surely other cases, but would be nice to solve those two.
Beta Was this translation helpful? Give feedback.
All reactions