That fact that the & operator is required for immutable pointers definitely
makes this change not-so-great unless we add the pass by value for
immutable.
For instance for C's memcpy which is defined in C as:
void *memcpy(void *dest, const void *src, size_t n)
is imported into Swift as:
public func memcpy(_: UnsafeMutablePointer<Void>, _: UnsafePointer<Void>,
_: Int) -> UnsafeMutablePointer<Void>
It would currently be called like:
var src: Float = 5.3
var dest: Float = 0.0
memcpy(&dest, &src, sizeof(Float))
If we don't change how UnsafePointer is called and just replace "&" with
"inout ", then it becomes:
memcpy(inout dest, inout src, sizeof(Float))
And now the "src" part of the call in now basically lying.
If we can just pass src directly (assuming that's what you meant by
pass-by-value), then it would be:
memcpy(inout dest, src, sizeof(Float))
which seems nicer than both the previous versions.
Would the UnsafePointer change need to be a proposal that is a predecessor
to this one?
P.S. It would be nice to be able to pass constants to UnsafePointer and not
using "&" for inout and UnsafePointer would allow that I think.
···
On Fri, Jan 29, 2016 at 6:23 PM, Jordan Rose <jordan_rose@apple.com> wrote:
+1 from me, but I suggest finding some real-world examples of inout (and
UnsafePointer) and showing those, rather than a made-up add1. The fact that
we currently require '&' for Unsafe-(immutable)-Pointer might make this a
bit less nice than it otherwise would be.
(We've talked about allowing pass-by-value for UnsafePointer, but that has
its own pros and cons, and should be discussed separately.)
Jordan
On Jan 29, 2016, at 14:44, Trent Nadeau via swift-evolution < > swift-evolution@swift.org> wrote:
https://github.com/tanadeau/swift-evolution/blob/master/proposals/00xx-use-inout-at-func-call-site.md
# Use `inout` at Function Call Sites
* Proposal: TBD
* Author(s): [Trent Nadeau](http://github.com/tanadeau\)
* Status: TBD
* Review manager: TBD
## Introduction
Currently when a function has `inout` parameters, the arguments are passed with the `&` prefix operator. For example:
func add1(inout num: Int) {
num += 1
}
var n = 5
add1(&n) // n is now 6
This operator does not fit with the rest of the language nor how the parameter is written at the function declaration. It should be replaced so that `inout` is used in both locations so that the call site above would instead be written as:
add1(inout n) // symmetric and now obvious that n can change
*Discussion thread TBD*
## Motivation
The `&` prefix operator is a holdover from C where it is usually read as "address of" and creates a pointer. While very useful in C due to its pervasive use of pointers, its meaning is not the same and introduces an unnecessary syntactic stumbling block from users coming from C. Removing this operator and using `inout` removes this stumbling block due to the semantic change.
This operator is also disconnected from how the function declaration is written and does not imply that the argument may (and likely will) change. Using `inout` stands out, making it clear on first read that the variable may change.
It is also possible that Swift may add Rust-like borrowing in the future. In that case, the `&` symbol would be better used for a borrowed reference. Note that Rust uses the same symbol for declaring a borrowed reference and creating one, creating a nice symmetry in that respect of the language. I think Swift would want to have such symmetry as well.
## Detailed design
in-out-expression → inout identifier
## Alternatives Considered
Keeping the syntax as it currently is.
--
Trent Nadeau
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
--
Trent Nadeau