Immutable reference to struct as param in function

Hello!
Is it possible to make immutable reference to struct as param in function?
For e.g., in rust:
struct Test{
x:i32
}
fn read_test_field(test: &Test) {
println!("{}",test.x);
}

Not yet. We do have this concept in the implementation, and at that level functions do generally expect their parameters to be "borrowed" this way. However, the semantics of arguments in Swift are that they're passed as copies, and passing them borrowed is just an optimization. There are many cases where this optimization works, but sometimes the compiler runs into the limits of what it can prove, and in these cases it must conservatively generate a copy and then borrow from that copy instead. I would eventually like to add some way for programmers to explicitly optimize their code by passing arguments as borrows, but for now there's no way to do it except it by writing code that's obvious enough to the optimizer.

Relatedly, we do hope to someday add non-copyable types to Swift, and to support them it will be necessary to let programmers distinguish between owned and borrowed parameters.

6 Likes