https://github.com/miroox/lvalueref
Pointer semantics simulation in Wolfram Language
https://github.com/miroox/lvalueref
pointers wolfram-language
Last synced: 9 months ago
JSON representation
Pointer semantics simulation in Wolfram Language
- Host: GitHub
- URL: https://github.com/miroox/lvalueref
- Owner: miRoox
- License: mit
- Created: 2019-01-18T03:08:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-01T02:13:43.000Z (almost 6 years ago)
- Last Synced: 2025-02-08T20:26:11.940Z (11 months ago)
- Topics: pointers, wolfram-language
- Language: Mathematica
- Homepage: https://miroox.github.io/2019/01/PointerInWolfram/
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ``LValueRef` ``
[](https://github.com/miRoox/LValueRef/actions)
Pointer semantics simulation in Wolfram Language.
* `Ref[lvalue]` refers to `lvalue`.
* `Deref[ref]` dereference.
* `RefQ[expr]` check if `expr` is a reference.
* `MakeRef[value]` create a reference with the `value`.
* `ExpandDerefAsLValue[expr]` expands dereference in the `expr` as lvalue.
### Usage
Load Package:
```mathematica
Needs["LValueRef`"]
```
Dereference:
```mathematica
a=1;
b=Ref[a];
Deref@b
(*Out[*]= 1*)
```
L-value:
```mathematica
a=1;
b=Ref[a];
Deref@b=2;
a
(*Out[*]= 2*)
```
Or more complicated:
```mathematica
a={1,2,3};
b=Ref[a];
(Deref@b)[[2]]=4;
a
(*Out[*]= {1,4,3}*)
```
Not only symbols can be referenced:
```mathematica
a={1,2,3};
b=Ref[a[[2]]];
Deref@b=4;
a
(*Out[*]= {1,4,3}*)
```
Multiple references:
```mathematica
a=1;
b=Ref[a];
c=Ref[b];
Deref@Deref@c=2;
a
(*Out[*]= 2*)
```
Implicit reference
(`MakeRef` is similar to `new` in Java):
```mathematica
a = MakeRef[1];
Deref[a]
(*Out[*]= 1*)
Deref[a] = 2;
Deref[a]
(*Out[*]= 2*)
```
### Possible alternatives
For simple cases:
```mathematica
a=1;
ra:=Unevaluated@@Hold[a]
f[r_]:=r=2
f[ra];
a
(*Out[*]= 2*)
```