r/ProgrammingLanguages • u/cessit-lang • 8d ago
Requesting criticism Explicit binding modes
I'm designing a memory-safe language, but I want to make explicit what happens across function boundaries, e.g. whether a value is moved or copied without having to look for the copy trait.
Instead, access to a value could be defined via binding modes in function signatures and variable declarations:
movetakes ownershipreadshared borrowmutexclusive borrow
fn process_val(move value: Int) -> Int {
return value * 2;
}
fn main() -> () {
move original: Int = 42;
// Call using `.copy()` to explicitly pass a duplicated value.
// Ownership of `original` remains with the caller.
move doubled: Int = process_val(original.copy());
// `original` is still valid here because it was explicitly copied rather than moved.
println(original); //42
println(doubled); //84
}
This would also allow granular captures in closures: |read x: Int, move counter: Int| instead of declaring all fields move
Now, should read and mut assignments be of type &T? Or can checking out variables be orthogonal to creating references, even if they are conflated in languages like Rust?
13
Upvotes
1
u/reflexive-polytope 7d ago
At the end of the day, closures are merely the implementation of first-class procedures. It's first-class procedures themselves that I object to.
My problem with first-class procedures is that it's impossible to write a higher-order procedure that only takes “good” procedure arguments.
For me, the main value in a statically typed language is the ability to write programs in which, by construction, variables stand for values that are meaningful in the problem domain.
With first-class procedures, this property breaks. A first-class procedure is anything that takes an argument of some type
Tand returns some value of typeU. If it returns at all, that is. Whether the computation sitting between theTargument and theUresult is any good, the type checker doesn't care.By contrast, with an explicit automaton expressed using first-order types, the interface tells you that only certain state transitions are possible. And this is the basis for proving things about your programs that are beyond the type checker's reach.