Use a unary operator for dereferencing
lordmauve opened this issue · 13 comments
Trying to use * for dereferencing doesn't work very well, because it isn't a unary operator. This means it only works in some contexts. Also it is "stealing" the iterability of the referenced object.
For example
xs = [1, 2, 3]
p_xs = to_ptr(xs)
print(**p_xs, sep="\n") # fails, splat + deref is treated as kwarg splat
A better deref operator would be unary +:
xs = [1, 2, 3]
p_xs = to_ptr(xs)
print(*+p_xs, sep="\n")
+ looks a bit like *, it just has one fewer spoke.
thanks for bringing this to my attention. i don't think + would be the best operator to use here, since people are generally used to that having to do something with addition. feel free to make a pr but if not ill implement this sometime tmr
The other options for unary operators are ~ and -.
Unary plus is barely used because it is a no-op for most numbers, so people don't have strong associations with it.
Unary minus is used a lot so it looks weird to use for this.
Bitwise complement is occasionally used but isn't recognisable as a mathematical symbol, so that might work too.
Using ~ is definitely the best option, you can implement this in a class with __invert__.
implemented in 1.0.7
As a sidenote, its also possible to implement Pointer.assign as an operator.
You could do something similar to myvalue >> pointer or myvalue >>= pointer and its also possible to implement the reverse, such as pointer << myvalue or pointer <<= myvalue.
Using this operator would call Pointer.assign and easily allow updating the pointer (works especially well for immutable values such as int, str, tuple.)
An example would be
myvalue = 5
p = Pointer(myvalue)
# adds 10 to *int
def add_ten(reference):
val = ~reference
val += 10
val >> reference
add_ten(p)
print(*p)
this isnt exactly how the library works. when you change the pointer, it changes the pointer itself, not the value its pointing to. i haven't implemented support for changing the data that its pointing to yet.
The reason for changing the address is for when you use an immutable value such as int and str
The proposed change would change the address (with Pointer.assign), not the value since for certain types, updating the value creates a copy at a new memory address. I'm not sure if there's any other way to fix that other than updating the value with Pointer.assign. The >> operator would be a shorthand for Pointer.assign.
sounds good, will implement
Be aware that you can add r to __lshift__ to create __rlshift__ for the reverse operator so it would be called if it's the first thing in the expression instead of the second. Same can be applied with adding i to create __ilshift__ to create <<=
im a bit confused on what the reverse operator would be, could you explain?
You should be able to find some information here: https://docs.python.org/3/library/operator.html. I would help implement it, but I'm not home right now. For example: if I have class1 + class2 it's going to execute class1's add but if class2 has radd it will execute that.
alright