Skip to content

Add missing information. #255

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions docs/language/attachments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ Attachments can be removed from a type in any order, so developers should take c

If a resource containing attachments is `destroy`ed, all of its attachments will be `destroy`ed in an arbitrary order.

## Attachment iteration

Attachments can be iterated over using the `forEachAttachment` function, a built-in function provided on types which support attachments. The signature is as follows:

```cadence
// for a struct
fun forEachAttachment(fun(&AnyStructAttachment)){}
// for a resource
fun forEachAttachment(fun(&AnyResourceAttachment)){}
```

The function takes a single argument, a callback function that accepts a reference to an attachment of the appropriate type. The callback function is called for each attachment present on the value, in an unspecified order.

For example, to iterate over all attachments on a resource `r`:

```cadence
r.forEachAttachment(fun(attachmentRef: &AnyResourceAttachment) {
// Do something with each attachment
})
```

<!-- Relative links. Will not render on the page -->

[access control]: ./access-control.md
Expand Down
2 changes: 1 addition & 1 deletion docs/language/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ entitlement UnpublishCapability

## Checking the existence of public capabilities

The function `capabilities.check` determines if a public capability was [published] at the given path before:
The function `capabilities.exists` determines if a public capability was [published] at the given path before:

```cadence
access(all)
Expand Down
117 changes: 117 additions & 0 deletions docs/language/core-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,123 @@ To reduce the potential for spam, we recommend that user agents that display eve

:::

### Storage Capability Controller Issued

Event that is emitted when a storage capability controller is created and issued to an account.

Event name: `flow.StorageCapabilityControllerIssued`

```cadence
access(all)
event StorageCapabilityControllerIssued(id: UInt64, address: Address, type: Type, path: Path)
```

| Field | Type | Description |
| --------- | --------- | ----------------------------------------------------------------------------- |
| `id` | `UInt64` | The ID of the issued capability controller |
| `address` | `Address` | The address of the account which the controller targets |
| `type` | `Type` | The kind of reference that be obtained with capabilities from this controller |
| `path` | `Path` | The storage path this controller manages |

### Account Capability Controller Issued

Event that is emitted when an account capability controller is created and issued to an account.

Event name: `flow.AccountCapabilityControllerIssued`

```cadence
access(all)
event AccountCapabilityControllerIssued(id: UInt64, address: Address, type: Type)
```

| Field | Type | Description |
| --------- | --------- | ----------------------------------------------------------------------------- |
| `id` | `UInt64` | The ID of the issued capability controller |
| `address` | `Address` | The address of the account which the controller targets |
| `type` | `Type` | The kind of reference that be obtained with capabilities from this controller |

### Storage Capability Controller Deleted

Event that is emitted when a storage capability controller is deleted.

Event name: `flow.StorageCapabilityControllerDeleted`

```cadence
access(all)
event StorageCapabilityControllerDeleted(id: UInt64, address: Address)
```

| Field | Type | Description |
| --------- | --------- | ----------------------------------------------------------------------------- |
| `id` | `UInt64` | The ID of the issued capability controller |
| `address` | `Address` | The address of the account which the controller targets |

### Account Capability Controller Deleted

Event that is emitted when an account capability controller is deleted.

Event name: `flow.AccountCapabilityControllerDeleted`

```cadence
access(all)
event AccountCapabilityControllerDeleted(id: UInt64, address: Address)
```

| Field | Type | Description |
| --------- | --------- | ----------------------------------------------------------------------------- |
| `id` | `UInt64` | The ID of the issued capability controller |
| `address` | `Address` | The address of the account which the controller targets |

### Storage Capability Controller Target Changed

Event that is emitted when a storage capability controller's path is changed.

Event name: `flow.StorageCapabilityControllerTargetChanged`

```cadence
access(all)
event StorageCapabilityControllerTargetChanged(id: UInt64, address: Address, path: Path)
```

| Field | Type | Description |
| --------- | --------- | ----------------------------------------------------------------------------- |
| `id` | `UInt64` | The ID of the issued capability controller |
| `address` | `Address` | The address of the account which the controller targets |
| `path` | `Path` | The new path this controller manages |

### Capability Published

Event that is emitted when a capability is published.

Event name: `flow.CapabilityPublished`

```cadence
access(all)
event CapabilityPublished(address: Address, path: Path, capability: Capability)
```

| Field | Type | Description |
| ------------ | ------------ | ----------------------------------------------------------------------------- |
| `address` | `Address` | The address of the account which the capability targets |
| `path` | `Path` | The path this capability is published at |
| `capability` | `Capability` | The published capability |

### Capability Unpublished

Event that is emitted when a capability is unpublished.

Event name: `flow.CapabilityUnpublished`

```cadence
access(all)
event CapabilityUnpublished(address: Address, path: Path)
```

| Field | Type | Description |
| ------------ | ------------ | ----------------------------------------------------------------------------- |
| `address` | `Address` | The address of the account which the capability targeted |
| `path` | `Path` | The path this capability was published at |

<!-- Relative links. Will not render on the page -->

[public key section]: ./crypto.mdx#public-keys
12 changes: 12 additions & 0 deletions docs/language/operators/casting-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ let boolean = something as! Bool
// Run-time error
```

## Implicit casting

Cadence provides support no support for implicit casting (coercion).

```cadence
let value: UInt8 = 1

// invalid: implicit cast
let intValue: Int = value
```


<!-- Relative links. Will not render on the page -->

[conditional downcasting operator `as?`]: #conditional-downcasting-operator-as
1 change: 1 addition & 0 deletions docs/language/pre-and-post-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Functions may have pre-conditions and may have post-conditions. They can be used
- A conditions block consists of one or more conditions. Conditions are expressions evaluating to a boolean.
- Conditions may be written on separate lines, or multiple conditions can be written on the same line, separated by a semicolon. This syntax follows the syntax for [statements].
- Following each condition, an optional description can be provided after a colon. The condition description is used as an error message when the condition fails.
- All conditions **must** appear as the first statements in a function definition and pre-conditions **must** be defined before post-conditions.

In post-conditions, the special constant `result` refers to the result of the function:

Expand Down
2 changes: 1 addition & 1 deletion docs/language/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ transaction {
}
```

Although optional, each phase serves a specific purpose when executing a transaction. It's recommended that developers use these phases when creating their transactions.
Although optional, each phase serves a specific purpose when executing a transaction. It's recommended that developers use these phases when creating their transactions. These phases **must** be defined in this order.

### Prepare phase

Expand Down
1 change: 1 addition & 0 deletions docs/language/types-and-type-system/run-time-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ view fun DictionaryType(key: Type, value: Type): Type?
// returns `nil` if `type` is not a reference type
view fun CapabilityType(_ type: Type): Type?
view fun ReferenceType(entitlements: [String], type: Type): Type?
view fun InclusiveRangeType(_ type: Type): Type?
```

## Asserting the type of a value
Expand Down
38 changes: 38 additions & 0 deletions docs/language/values-and-types/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,44 @@ The following functions can only be used on variable-sized arrays. It is invalid
numbers.removeLast()
```

-
```cadence
access(all)
fun toConstantSized<[T; N]>(): [T; N]?
```

Converts a variable-sized array to a constant-sized array if the number of elements matches the target size `N` and the original variable-sized array has type `T`. Available if `T` is not resource-kinded.

Returns nil if the number of elements does not match the target size `N`.

```cadence
// Declare an array of integers.
let numbers = [42, 23]

// Change to a constant-sized array
let numbersConst = numbers.toConstantSized<[Int; 2]>()
// numbersConst has the type [Int; 2]?
```

## Constant-size array functions

-
```cadence
access(all)
fun toVariableSized(): [T]
```

Converts a constant-sized array to a variable-sized array of the same type `T`. Available if `T` is not resource-kinded.

```cadence
// Declare an array of integers.
let numbers: [Int16; 3] = [1, 2, 3]

// Change to a constant-sized array
let numbersVar = numbers.toVariableSized()
// numbersVar has the type [Int]
```

<!-- Relative links. Will not render on the page -->

[mutates]: ../access-control.md
21 changes: 20 additions & 1 deletion docs/language/values-and-types/dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,26 @@ Dictionary keys must be hashable and equatable.

Most of the built-in types, like booleans and integers, are hashable and equatable, so can be used as keys in dictionaries.

A comprehensive list of valid dictionary key types:
- [Address]
- [Bool]
- [Character]
- [Enum]
- [Numbers]
- [Paths]
- [Runtime-types]
- [String]


<!-- Relative links. Will not render on the page -->

[optional]: ./anystruct-anyresource-opts-never.md#optionals
[mutates]: ../access-control.md
[mutates]: ../access-control.md
[Address]: ./addresses-functions.md
[Enum]: ../enumerations.md
[Bool]: ./booleans-numlits-ints.md#booleans
[Character]: ./strings-and-characters.md
[String]: ./strings-and-characters.md
[Runtime-types]: ../types-and-type-system/run-time-types.md
[Numbers]: ./booleans-numlits-ints.md#numeric-literals
[Paths]: ../accounts/paths.mdx
44 changes: 43 additions & 1 deletion docs/language/values-and-types/fixed-point-nums-ints.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,48 @@ All fixed-point types support the following functions:
let negativeNumber: Fix64? = Fix64.fromBigEndianBytes([255, 255, 255, 255, 250, 10, 31, 0]) // ok, -1
```

## Number type casting

When casting between number types (e.g. `Int` to `UInt`, `Fix64` to `Int`) you cannot use the [casting operators] (`as`, `as?` and `as!`), you must explicitly call the constructor of the desired type (e.g. `UInt(_)`).

```cadence
let value: UInt8 = 1

let intValue: Int? = value as? Int
// intValue is `nil` and has type `Int?`

let validInt: Int = Int(value)
// validInt is `1` and has type `Int`
```

When casting from a larger number type to a smaller one (narrowing), the cast will succeed if the value can be represented in the smaller type. If it cannot an error will be thrown indicating overflow or underflow. Casting to a larger number type will always succeed.

```cadence
let intValue: Int16 = 256

let uintValue: UInt8 = UInt8(intValue)
// error: overflow, UInt8 has max value of `255`

let validUInt: UInt16 = UInt16(intValue)
// validUInt is `256` and has type `UInt16`

let largerIntValue: Int = Int(intValue)
// largerIntValue is `256` and has type `Int`
```

You can cast from integer types to fixed point types and vice versa by calling the constructor as well. The same conditions as narrowing applies, an error will be thrown if the value cannot be represented in the range.

```cadence
let intValue: Int = -1

let fixValue: Fix64 = Fix64(intValue)
// fixValue is `-1.00000000` and has type `Fix64`

let ufixValue: UFix64 = UFix64(intValue)
// error: underflow, UFix64 has min value `0.0`
```

<!-- Relative links. Will not render on the page -->

[optional]: ./anystruct-anyresource-opts-never.md#optionals
[optional]: ./anystruct-anyresource-opts-never.md#optionals
[casting operators]: ../operators/casting-operators
5 changes: 4 additions & 1 deletion docs/language/values-and-types/inclusive-range.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,8 @@ A value of type `InclusiveRange<T>`, where `T` is a number type, has the followi
contains = range.contains(11)
```

<!-- Relative links. Will not render on the page -->
## Usage in loops
See [Ranges in loops] for more information.

<!-- Relative links. Will not render on the page -->
[Ranges in loops]: ../control-flow.md#ranges-in-loops
37 changes: 37 additions & 0 deletions docs/language/values-and-types/strings-and-characters.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,43 @@ Strings have multiple built-in functions you can use:
example.split(separator: " ") // is `["hello", "world"]`
```

- ```cadence
view fun count(_ substr: String): Int
```

Returns the number of times the provided `substr` appears in the string. This function only counts each character once (see below).

```cadence
let example = "11111"

example.count("11") // is 2
```

- ```cadence
view fun index(of: String): Int
```

Returns the index of the first (leftmost) occurrence of `of` in the string or `-1` if `of` does not occur in the string.

```cadence
let example = "abcabc"

example.index(of: "a") // is 0
example.index(of: "d") // is -1
```

- ```cadence
view fun contains(_ other: String): Bool
```

Returns `true` if the string contains `other`, `false` otherwise.

```cadence
let example = "abcdef"

example.contains("abcdefg") // is false
```

The `String` type also provides the following functions:

- ```cadence
Expand Down