What can Rust's type/lifetime system do that Val can't? #788
-
I've always been surprised by the common practice in Rust of giving lifetimes a single-letter name. Basically every example of Rust that contains lifetimes calls them I've been reading through the wiki and the discussions here, and I still don't fully understand the language. My understanding so far is that references are second-class citizens in Val. You can't directly create one, but you can use parameter intents and remotes, which result in a reference behind the scenes. What I don't understand is how Val can distinguish between something being passed as 'inout' because you want to modify it, and something being passed as 'inout' because you want to alias it. For example, suppose you have a 'deserialize' method which creates a 'Message' from an array of bytes. For performance reasons, the message content points to the original array instead of copying it into a new one. This method also utilizes caching, so it needs the cache to be passed in as a mutable parameter. In Rust, I could have something like the following: fn deserialize<'a, 'b>(frame: &'a mut [i8], cache: &'b mut Cache) -> Message<'a> {
// ...
} Please excuse any syntax errors, I'm a C# developer but I hope the example demonstrates the point The lifetime annotations make it very clear that I am returning something which aliases the content, not the cache. Rust understands that after this function has been called, the contents are aliased by the returned message but it's safe to use the cache in another call. Is it possible to achieve the same thing in Val? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
To be fair, you can use any name you want. The single-letter practice is just a convention, like using
You never use
You can't return a reference to a part of an object. However, you can project it. This mechanism plays a big role in the elimination of lifetime annotations because a projected value can never escape. The signature of
Note: I don't think
Because we're only projecting a It is possible to work around the type checker, but not in the safe subset of the language. |
Beta Was this translation helpful? Give feedback.
-
I dont know, should I open a new topic, because my question relates to this topic. What if I has an outer module with subscript:
And has a main module with code:
Should it compile? Can compiler recognize that result |
Beta Was this translation helpful? Give feedback.
To be fair, you can use any name you want. The single-letter practice is just a convention, like using
T
for most generic type parameters.You never use
inout
to alias anything. In fact, you never use any passing convention to talk about aliasing. Val is all about independent values and lets you write programs as though references didn't exist.