Skip to content
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

Merged
merged 6 commits into from
Nov 30, 2021
Merged

feat: add box type #1358

merged 6 commits into from
Nov 30, 2021

Conversation

scolsen
Copy link
Contributor

@scolsen scolsen commented Nov 19, 2021

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.

@scolsen scolsen requested a review from a team November 19, 2021 21:37
@TimDeve
Copy link
Contributor

TimDeve commented Nov 21, 2021

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 Box.peek : (Fn [(Ref (Box a))] (Ref a)) so you can do things like this without re-alloc:

(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.

@scolsen
Copy link
Contributor Author

scolsen commented Nov 22, 2021

That's correct! I'll try to add in Box.peek!

@scolsen
Copy link
Contributor Author

scolsen commented Nov 29, 2021

Ok, added Box.peek!

(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)

scolsen and others added 6 commits November 29, 2021 10:22
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!
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]>
@scolsen
Copy link
Contributor Author

scolsen commented Nov 29, 2021

The nix failure stems from a nix-install script failure, not from the tests.

@eriksvedang eriksvedang merged commit 380945b into carp-lang:master Nov 30, 2021
@eriksvedang
Copy link
Collaborator

Very nice, let's ship it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants