Closed MovingtoMars closed 8 years ago
Hopefully, with this setup, the compiler will be able optimise out many ref count checks. Also, it could even replace some ARC pointers with raw pointers when it is proven to be safe.
LGTM, perhaps in the casting from_raw_ptr
and to_raw_ptr
, we could put this inside arc.ark
so in effect you do arc::from_raw_ptr
or something like that. Not particularly sure on the @
symbol for raw pointers, any particular reason why you chose this over something like an asterisks *
?
It's to make it consistent with the other two, where the type symbol is the same as the address-of operator. Keeping *
separate from all three keeps it clean.
@MovingtoMars Ah yeah the dereferencing stuff, hm yeah I guess I like most of this though if we could pick a symbol for raw pointers could it be like '
, so x: 'int = ...
. I'm not too bothered, but if the opportunity is there...
Ideally, you shouldn't be using raw pointer too often, so it shouldn't matter.
This seems a bit overcomplicated, couldn't all references be reference counted, rather than having a totally distinct type for that?
@SamTebbs33 The idea is it's a systems language, you don't want to reference count everything because:
Though I do agree that introducing 3 new types is a little overcomplicated for my liking.
@MovingtoMars Yah but the @
symbol seems a little arbitrary, and if we're going for arbitrary I'd rather it be foo: 'T = ...
:wink:
I'll start out with saying that in general, I think this is a really complicated approach, and ideally I'd want just raw pointers and then do (A)RC completely in the stdlib, mainly via the implementation of RAII-like system using interfaces (e.g a Drop
interface containing a method that would be invoked once the value leaves scope).
Now with that out of the way, my comments on this approach:
@n
was the ARC pointer vs ^n
as this would be more of an indications that "this is not just a pointer".*
as a generic dereference operator seems okay.mem::arc_from_raw_pointer
to create an ARC pointer, but how would you go about handling the deallocation once that happens?@kiljacken I mostly agree, I would prefer arc in the stdlib rather than introducing loads of weird pointer syntax.
Ok. It'll probably be a lot less complicated by putting it in the stdlib.
These are some changes to simplify syntax and semantics.
Types
By default, these types are immutable. Mutable variations:
Address-of
Raw pointers and referecnes can be created by using the same symbol used in the type as the address-of operator. There is no address-of operator for ARC pointers.
Note that you cannot take a raw pointer to a stack variable. You can bypass this restruction like so:
raw_ptr := @int(uintptr(&thing));
Dereferencing
There is one deference operator to rule them all:
*
.Casting
Raw pointers and references can be casted to uintptr and back.
To get a raw pointer from an ARC pointer, use the library function
mem::arc_get_raw_pointer<T>(ptr: ^T) -> @T
.To get an ARC pointer from a raw pointer, use the library function
mem::arc_from_raw_pointer<T>(ptr: @T, refs int) -> ^T
.Raw pointers and ARC pointers can be implicitly casted to references. This is useful for functions which should be able to take all three types.
Also, all 3 mutable type variations can be implicitly casted to their immutable variations.