Baseline: tree as shared
This commit is contained in:
1679 files changed
+1024562
No files matched your search
@@ -0,0 +1,87 @@
|
||||
def append_twice(mut s: String, other: String):
|
||||
# Mojo knows 's' and 'other' can't be the same string.
|
||||
s += other
|
||||
s += other
|
||||
|
||||
def invalid_access():
|
||||
var my_string = "o" # Create a run-time String value
|
||||
|
||||
# error: passing `my_string` mut is invalid since it's also passed
|
||||
# as an immutable reference
|
||||
append_twice(my_string, my_string)
|
||||
print(my_string)
|
||||
```
|
||||
|
||||
This code is confusing because the user might expect the output to be `ooo`,
|
||||
but since the first addition mutates both `s` and `other`, the actual output
|
||||
would be `oooo`. Enforcing exclusivity of mutable references not only prevents
|
||||
coding errors, it also allows the Mojo compiler to optimize code in some cases.
|
||||
|
||||
One way to avoid this issue when you do need both a mutable and an immutable
|
||||
reference (or need to pass the same value to two arguments) is to make a copy:
|
||||
|
||||
```mojo
|
||||
def valid_access():
|
||||
var my_string = "o" # Create a run-time String value
|
||||
var other_string = my_string # Create a copy of the String value
|
||||
append_twice(my_string, other_string)
|
||||
print(my_string)
|
||||
```
|
||||
|
||||
Note that argument exclusivity isn't enforced for register-passable trivial
|
||||
types (like `Int` and `Bool`) as they're always passed by copy. When
|
||||
passing the same value into two `Int` arguments, the callee receives two
|
||||
copies of the value.
|
||||
|
||||
## Transfer arguments (`var` and `^`)
|
||||
|
||||
If you want your function to take *ownership* of a value, add the `var`
|
||||
keyword before the argument name.
|
||||
|
||||
This convention is often combined with using the postfix `^` transfer sigil
|
||||
on an argument at the call site.
|
||||
|
||||
When using a variable, transferring a value leaves the original variable
|
||||
uninitialized. You can't use the variable after the transfer until you
|
||||
assign it a new value of the original type.
|
||||
|
||||
### Transferring with `var`
|
||||
|
||||
`var` behaves differently depending on whether the caller uses the `^`
|
||||
transfer sigil and whether the value conforms to `Copyable`.
|
||||
|
||||
The `var` keyword doesn't guarantee that the function receives *the original
|
||||
value*. It guarantees only that the function receives *ownership of a
|
||||
value*. That happens in one of three ways:
|
||||
|
||||
- **Value transfer**: The caller uses the `^` transfer sigil. This
|
||||
transfers the value, leaving the original variable uninitialized. The function
|
||||
argument receives ownership.
|
||||
|
||||
- **Copying**: Without the transfer sigil, Mojo copies the value. If the
|
||||
type isn't `Copyable`, this produces a compile-time error.
|
||||
|
||||
- **Newly created value**: The caller passes a newly created value,
|
||||
such as the result of a function call. In this case, no variable owns the
|
||||
value, so ownership transfers directly to the callee. For example:
|
||||
|
||||
```mojo
|
||||
def take(var s: String):
|
||||
pass
|
||||
|
||||
def main():
|
||||
take("A brand-new String!")
|
||||
```
|
||||
|
||||
The following code works by making a copy of the string, because `take_text()`
|
||||
uses the `var` convention, and the caller doesn't include the transfer sigil:
|
||||
|
||||
```mojo
|
||||
def take_text(var text: String):
|
||||
text += "!"
|
||||
print(text)
|
||||
|
||||
def main():
|
||||
var message = "Hello" # Create a run-time String value
|
||||
take_text(message)
|
||||
print(message)
|
||||
Reference in new issue
Block a user