A bit related to this thread: #ideas > linear types
For example, a bit like this:
fn = |x,y,z| {
......
.....
drop(z) <- var name z should not be ref/declared in this scope anymore
g(a, b, z) <- compiler should complain here, as the binding on z should be gone now.
}
Can you elaborate a bit? I don't understand the question.
Steve Howell said:
Can you elaborate a bit? I don't understand the question.
What I mean is, is it possible to make accidental use of a dropped/freed binding a compiler error, rather than just letting it be syntaxly correct.
It's a bit like Semi-linear type.
The part I don't understand is the code sample. I don't know what you mean by free/drop there. I think I agree with your premise, but it's still not clear to me.
Steve Howell said:
The part I don't understand is the code sample. I don't know what you mean by free/drop there. I think I agree with your premise, but it's still not clear to me.
Well, tried to improve the example a bit. Hope it's clearer.
Roc is memory safe, and uses automatic reference counting under the hood; it's not possible to accidentally use after free etc. If you find anything that is use after free, that is a compiler bug
If you modify something that has multiple references to it, Roc will clone the value and give you another copy.
So roc is semantically immutable -- any in-place mutation optimisation is automatic.
Luke Boswell said:
Roc is memory safe, and uses automatic reference counting under the hood; it's not possible to accidentally use after free etc. If you find anything that is use after free, that is a compiler bug
Yes, I guess the keyword I used here is a bad pick.
What I am trying to do here is more about preventing human mistake. Say I have x, and y of the same type, y = g(f(x)). I would like to make sure I used y which has updated value afterwards. Is there a way to label x s.t. after that point in this scope, trying to use results a compiler error? Because without it, using x can still be syntaxly correct while the computation under the hood is entirely wrong.
For the example above, normally, x,y,z can be refed anywhere inside fn as parameters should always be in scope inside function body. With early ending on binding, I could make them inaccessable after a point in the function body.
There isn't a way to do that in Roc syntax.
But there are tools floating around which can help you.
My favourite is to write tests using roc-fuzz and use the builtin allocation measurements to help me do that. I can write a test that says "guarantee this function makes no allocations" and then if it fails I know I have violated that by leaving a stray reference around.
Luke Boswell said:
There isn't a way to do that in Roc syntax.
But there are tools floating around which can help you.
My favourite is to write tests using
roc-fuzzand use the builtin allocation measurements to help me do that. I can write a test that says "guarantee this function makes no allocations" and then if it fails I know I have violated that by leaving a stray reference around.
Well, never knew fuzz can do that, will use it to enforce RC=1.
It feels like it got valgrinds feature then.
Or for some things where I've been migrating "legacy" code written in roc (feels ironic to say) -- I use roc-fuzz to add a ratchet around some functions, so we keep a baseline measurement and keep reducing that as we improve the implementation.
zhen said:
Well, never knew fuzz can do that, will use it to enforce RC=1 then
Yeah it's kind of grown over time, and now does way more than just "fuzzing". I've been improving it as I learn things or get ideas from experience using it.
It's now more like a swiss-army knife for pure Roc correctness and performance things.
The latest Model and Solve features are putting in work on roc-pdf right now, flushing out all kinds of bugs.
wasn’t there an allowed way to shadow a local variable at some point @Luke Boswell ? This way you are guaranteed to not re-use the previous one, even if shadowing has its own issues. Or is that gone? Not sure cause I haven’t written roc for quite some time.
Maybe that is using var $thing = ...
you can use a sub block to kinda do this, which is in it's own scope
x = {
a = 1
{
b = 2
_ = b
}
# here b is not accessible
_ = a
}
Jared Ramirez said:
you can use a sub block to kinda do this, which is in it's own scope
x = { a = 1 { b = 2 _ = b } # here b is not accessible _ = a }
Right technically this will do
Last updated: Nov 26 2026 at 20:35 UTC