flan/test/programs/grow-param.flan

24 lines
856 B
Plaintext

;;;; A container parameter is a copy of the caller's header. Growing it grows
;;;; the copy, so the caller's container does not see the push; the function
;;;; is warned at, at the parameter, and the fix it names is the (Ptr ...)
;;;; below, which reaches the caller's own header.
(defn add-copy [v (Vec i32)] () (push v 1) (free v))
(defn add-ptr [v (Ptr (Vec i32))] () (push (deref v) 2))
(defn put-ptr [m (Ptr (Map i32 i32))] () (put (deref m) 7 8))
(defn main [] i32
(let [v (vec-new i32)
m (map-new i32 i32)]
(add-copy v)
(println (length v)) ; 0
(add-ptr (addr v))
(add-ptr (addr v))
(println (length v)) ; 2
(println (at v 1)) ; 2
(put-ptr (addr m))
(println (length m)) ; 1
(free v)
(free m))
0)