Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make part 1, 2, and 3 of Cadence Tutorial more interactive and streamlined #172

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/language/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,43 @@ dictionaries[false][3] = 0
//}`
```

## Force-assignment operator (`<-!`)
## Move Operator (`<-`)

The move operator (`<-`) is unique to Cadence and is used to move [resource types](./resources.mdx) from one location to another. It works similar to the assignment operator (`=`) you're used to from most programming languages, except that the data in the location on the right side of the statement is **destroyed** by the operation.

```cadence
// Declare a resource named `SomeResource`, with a variable integer field.

access(all)
resource SomeResource {

access(all)
var value: Int

init(value: Int) {
self.value = value
}
}

// Declare a constant with value of resource type `SomeResource`.

let a: @SomeResource <- create SomeResource(value: 5)

// *Move* the resource value to a new constant.

let b <- a

// Invalid Line Below: Cannot use constant `a` anymore as the resource that it
// referred to was moved to constant `b`.

a.value

// Constant `b` owns the resource.

b.value // equals 5
```

## Force-assignment Operator (`<-!`)

The force-assignment operator (`<-!`) assigns a resource-typed value
to an optional-typed variable if the variable is nil.
Expand Down
41 changes: 18 additions & 23 deletions docs/language/resources.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ Resources **must** be created (instantiated) by using the `create` keyword.
At the end of a function which has resources (variables, constants, parameters) in scope,
the resources **must** be either **moved** or **destroyed**.

They are **moved** when used as an initial value for a constant or variable,
when assigned to a different variable,
when passed as an argument to a function,
and when returned from a function.
They are **moved** when used as an initial value for a constant or variable, when assigned to a different variable, when passed as an argument to a function, and when returned from a function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below: It looks like only the newlines/line breaks got removed from the original content.

The line breaks are intentional, they are "Semantic Line Breaks": They are ignored in the rendered output, but are useful in the source Markdown, because they keep lines limited to a certain length (<=100) like in source code, and make it easier to review changes.

See https://sembr.org/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've worked with both methods before and find removing semantic line breaks to generally improve workflow for writing, editing, and updating docs.

Most editors can be set to wrap the text when viewing it, and by not having the line breaks it's much easier to edit and wordsmith the text because updates automatically display similar to how they'd appear on the page without having to save to autoformat, or run a script to rebreak at 80 wide.


Resources can be explicitly **destroyed** using the `destroy` keyword.

Accessing a field or calling a function of a resource does not move or destroy it.

When the resource is moved, the constant or variable
that referred to the resource before the move becomes **invalid**.
An **invalid** resource cannot be used again.
When the resource is moved, the constant or variable that referred to the resource before the move becomes **invalid**. An **invalid** resource cannot be used again.

To make the usage and behaviour of resource types explicit,
the prefix `@` must be used in type annotations
Expand All @@ -39,7 +34,7 @@ and when it is returned from a function.

```cadence
// Declare a resource named `SomeResource`, with a variable integer field.
//

access(all)
resource SomeResource {

Expand All @@ -52,38 +47,38 @@ resource SomeResource {
}

// Declare a constant with value of resource type `SomeResource`.
//
let a: @SomeResource <- create SomeResource(value: 0)

let a: @SomeResource <- create SomeResource(value: 5)

// *Move* the resource value to a new constant.
//

let b <- a

// Invalid: Cannot use constant `a` anymore as the resource that it referred to
// was moved to constant `b`.
//
// Invalid Line Below: Cannot use constant `a` anymore as the resource that it
// referred to was moved to constant `b`.

a.value

// Constant `b` owns the resource.
//
b.value // equals 0

b.value // equals 5

// Declare a function which accepts a resource.
//

// The parameter has a resource type, so the type annotation must be prefixed with `@`.
//

access(all)
fun use(resource: @SomeResource) {
// ...
}

// Call function `use` and move the resource into it.
//

use(resource: <-b)

// Invalid: Cannot use constant `b` anymore as the resource
// it referred to was moved into function `use`.
//
// Invalid Line Below: Cannot use constant `b` anymore as the resource it
// referred to was moved into function `use`.

b.value
```

Expand All @@ -93,7 +88,7 @@ The program must either explicitly destroy it or move it to another context.
```cadence
{
// Declare another, unrelated value of resource type `SomeResource`.
//

let c <- create SomeResource(value: 10)

// Invalid: `c` is not used before the end of the scope, but must be.
Expand Down
4 changes: 2 additions & 2 deletions docs/solidity-to-cadence.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Guide for Solidity Developers
sidebar_label: Guide for Solidity Developers
title: Cadence Guide for Solidity Developers
sidebar_label: Cadence Guide for Solidity Developers
sidebar_position: 8
---
Cadence introduces a different way to approach smart contract development which may feel unfamiliar to Solidity
Expand Down
Loading