Weak values can be usefull sometimes. What I miss with it though is that the flag is lost when during operations (add, sub...) It would be nice to be able to do: a += b; // => a = a + b; without losing the weak flag on array a.
Any thoughts on this?
The things is though, that it isn't array a which loses the weak flag. Addition of two arrays creates a _completely new_ array, which is then stored in the variable a. Since the weak flag is on the array and not the variable, a will refer to a weak array after the operation only if the newly created array is also weak.
So, should addition between a weak array and another (weak?) array also yield a weak array? We have three possible cases:
1) weak + non-weak 2) non-weak + weak 3) weak + weak
At least for case 1 and 3, getting a weak array back seems useful, but I'm not sure there are no pitfalls that could result from this...
Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
The things is though, that it isn't array a which loses the weak flag. Addition of two arrays creates a _completely new_ array, which is then stored in the variable a. Since the weak flag is on the array and not the variable, a will refer to a weak array after the operation only if the newly created array is also weak.
So, should addition between a weak array and another (weak?) array also yield a weak array? We have three possible cases:
- weak + non-weak
- non-weak + weak
- weak + weak
At least for case 1 and 3, getting a weak array back seems useful, but I'm not sure there are no pitfalls that could result from this...
I'm aware of this. Perhaps have a way of operating with a weak-valued object to flag that you want the resulting (newly created objects) to inherit any (and all) weak keys/values present in the operation, regardles of case 1, 2 or 3? Not sure what this would look like though, but it would introduce some kind of new syntax, leaving the old syntax as it is today (without new pitfalls).
Uhm... or maybe I should simply make a new class simulating the array type, overloading the `+ `- etc operations. I can see that there might be cases when you don't want the flag to be passed along, but in my case, I always want it for all such operations I do on the array.
//K
could you please explain what a weak value is? my cmod coding skills are not strong enough ;-)
greetings, martin.
Martin Bähr wrote:
could you please explain what a weak value is? my cmod coding skills are not strong enough ;-)
greetings, martin.
hasn't any to do with cmod's :) It's about the garbage collector. It lets you keep values in a table (array, mapping or multiset, see: http://pike.ida.liu.se/generated/manual/modref/ex/predef_3A_3A/set_weak_flag... ) without adding the extra reference, so when there is no one else referencing the item, it gets flagged as garbage, and will be destructed. Quite handy ;o) works for both keys and/or values. There's probably more info available on the net if you search on weak values, it's not a unique property of pike ;o)
//K
pike-devel@lists.lysator.liu.se