20956, "jeremiah-corrado", "'weakPointer': how should the type be represented in the language?", "2022-11-02T13:51:48Z"
opened 01:51PM - 02 Nov 22 UTC
The [prototype implementation](https://github.com/chapel-lang/chapel/pull/20918/… files) for `weakPointer`, implements the pointer as a generic record type:
```chapel
record weakPointer {
type t
...
}
```
which is defined a submodule of `sharedObject`. This type can be imported and used as follows:
```chapel
use WeakPointer;
// or import WeakPointer.weakPointer;
var myShared = new shared someClassType();
var my_weak = new weakPointer(myShared);
```
This approach is similar to Rust's `weak` and C++'s `weak_ptr`; however it differs from the other memory management strategies in the language because they are implemented using keywords rather than exposed types.
---
There are a few questions here:
- Would it be better to expose it to users as a keyword (more like `shared`, `owned`, `borrowed`)? This is more similar to Swift's interface. Or to keep it as a generic type?
- If we keep it as a type in the standard library, where should it belong? Should there be a new module for pointer types, can it go in an existing module, or is the current location ok?
- In either case, what should the type/keyword be named?
The prototype implementation for weakPointer, implements the pointer as a generic record type:
record weakPointer {
type t
...
}
which is defined a submodule of sharedObject. This type can be imported and used as follows:
use WeakPointer;
// or import WeakPointer.weakPointer;
var myShared = new shared someClassType();
var my_weak = new weakPointer(myShared);
This approach is similar to Rust's weak and C++'s weak_ptr; however it differs from the other memory management strategies in the language because they are implemented using keywords rather than exposed types.