-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add box type #1358
feat: add box type #1358
Conversation
So you've split out the implementation of Box from the recursive type implementation, correct? It looks good to me. I would maybe just add something like (deftype Foo [bar Int])
(defn main []
(let-do [b (Box.init (Foo.init 0))]
(Foo.set-bar! (Box.peek &b) 42)
(println* b))) But I'm also ok with keeping this PR MVPish and add stuff later if we need. |
That's correct! I'll try to add in |
Ok, added (note: I also forgot to include Veit as a co-author on the original commit (he fixed the string templates!) I've fixed this an forced pushed the amended commit) |
This commit adds an implementation of Boxes, memory manged heap allocated values. Boxes are implemented as C pointers, with no additional structure but are treated as structs in Carp. To facilitate this, we need to add them as a clause to our special type emissions (TypesToC) as they'd otherwise be emitted like other struct types. Co-authored-by: Veit Heller <[email protected]>
Make sure we free the box!
This reverts commit 70ec6d4.
Now that a builtin type named Box exists, the definitions in this file cause a conflict. I've renamed the "Box" type in the functor example to remove the conflict.
Box.peek allows users to transform a reference to a box into a a reference to the box's contained value. The returned reference will have the same lifetime as the box. This function allows callers to manipulate the value in a box without re-allocation, for example: ```clojure (deftype Num [val Int]) (let-do [box (Box.init (Num.init 0))] (Num.set-val! (Box.peek &box) 1) @(Num.val (Box.peek &box))) ``` This commit also includes tests for Box.peek. Co-authored-by: TimDeve <[email protected]>
The nix failure stems from a |
Very nice, let's ship it! |
This PR adds a Box type to Carp using the initial approach in #1337.
One important difference is that the box is implemented as a pointer, instead of a struct. While this is simpler, it doesn't play quite as well with Carp's overall programming model, since we need to treat the type specially in
TypesToC
as a result--but then again, we already do this for some other compiler defined types so it's not too strange.