capture an array and a const local
C copies an array neither by = nor from another array's initializer, and a const field cannot be assigned: both failed in the C. An array is copied in element by element; a const one is held as its value.
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
"Nested: 33"
|
||||
"Named capture: 7"
|
||||
"Captured pointer: 11 then 12"
|
||||
"From a bare fn: 20 42 7")
|
||||
"From a bare fn: 20 42 7"
|
||||
"Captured array: 6 3, still 6"
|
||||
"Captured const: 6")
|
||||
(return 0)
|
||||
|
||||
;;; A closure is a code pointer beside its captures, so what this
|
||||
@@ -118,4 +120,19 @@
|
||||
(apply-twice from-fn 5)
|
||||
((as-closure) 21)
|
||||
(apply-twice (lambda ((n int)) int (return (+ n 1))) 5))
|
||||
|
||||
;; an array is copied in, element by element, as C copies one neither
|
||||
;; by = nor into a parameter; so are the rows of one of arrays
|
||||
(var xs [int 3] #(1 2 3))
|
||||
(var m [int 2 2] #(#(1 2) #(3 4)))
|
||||
(var sum3 (closure () int) (closure () int (xs) (return (+ [xs 0] [xs 1] [xs 2]))))
|
||||
(var corner (closure () int) (closure () int (m) (return [m 1 0])))
|
||||
(printf "Captured array: %d %d" (sum3) (corner))
|
||||
(= [xs 0] 100)
|
||||
(printf ", still %d\n" (sum3))
|
||||
|
||||
;; a const local is captured as its value, and stays const inside
|
||||
(var k (const int) 5)
|
||||
(var plus-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k))))
|
||||
(printf "Captured const: %d\n" (plus-k 1))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user