mirror of
https://github.com/carp-lang/Carp.git
synced 2024-10-11 04:27:55 +03:00
Setting variables by reference, using @ works now.
This commit is contained in:
parent
06248af2f8
commit
c90357ac6d
25
examples/setting_variables.carp
Normal file
25
examples/setting_variables.carp
Normal file
@ -0,0 +1,25 @@
|
||||
(defn changing-target-of-ref []
|
||||
(let [s1 @"hello"
|
||||
s2 @"goodbye"
|
||||
r &s1]
|
||||
(do (set! r &s2)
|
||||
(IO.println r))))
|
||||
|
||||
(defn set-derefed []
|
||||
(let [s @"hello"
|
||||
r &s]
|
||||
(do (set! @r @"new")
|
||||
(IO.println &s))))
|
||||
|
||||
(defn f [r]
|
||||
(set! @r 100))
|
||||
|
||||
(defn set-in-parameter []
|
||||
(let [x 1]
|
||||
(do (f &x)
|
||||
(IO.println &(str x)))))
|
||||
|
||||
(defn-do main []
|
||||
(changing-target-of-ref)
|
||||
(set-derefed)
|
||||
(set-in-parameter))
|
@ -8,20 +8,3 @@
|
||||
;; (Bucket.init 0 []))
|
||||
;; (defn f []
|
||||
;; (the (Bucket Float Bool) (empty))) ;; run checks in the concretizer to fix this
|
||||
|
||||
|
||||
(defn changing-target-of-ref []
|
||||
(let [s1 @"hello"
|
||||
s2 @"goodbye"
|
||||
r &s1]
|
||||
(do (set! r &s2)
|
||||
(IO.println r))))
|
||||
|
||||
(defn set-derefed []
|
||||
(let [s @"hello"
|
||||
r &s]
|
||||
(do (set! @r @"new")
|
||||
(IO.println &s))))
|
||||
|
||||
(defn main []
|
||||
(set-derefed))
|
||||
|
@ -13,6 +13,7 @@ carp ./examples/updating.carp -x;
|
||||
carp ./examples/sorting.carp -x;
|
||||
carp ./examples/globals.carp -x --log-memory;
|
||||
carp ./examples/generic_structs.carp -x;
|
||||
carp ./examples/setting_variables.carp -x;
|
||||
|
||||
# Actual tests (using the test suite)
|
||||
carp ./test/memory.carp -x --log-memory;
|
||||
|
@ -487,7 +487,8 @@ manageMemory typeEnv globalEnv root =
|
||||
|
||||
let varInfo = info variable
|
||||
correctVariable = case variable of
|
||||
XObj (Lst (XObj Ref _ _ : x : _)) _ _ -> x -- Peek inside the ref to get the actual variable to set
|
||||
--XObj (Lst (XObj Ref _ _ : x : _)) _ _ -> x -- Peek inside the ref to get the actual variable to set
|
||||
(XObj (Lst (XObj (Sym (SymPath _ "copy") _) _ _ : symObj@(XObj (Sym sym _) _ _) : _)) _ _) -> symObj
|
||||
x -> x
|
||||
|
||||
MemState managed deps <- get
|
||||
|
Loading…
Reference in New Issue
Block a user