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

Add view annotations to view library functions #33

Merged
merged 3 commits into from
Dec 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion versioned_docs/version-1.0/language/accounts/contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct DeployedContract {
/// ```
/// then `.publicTypes()` will return an array equivalent to the expression `[Type<Bar>(), Type<Qux>()]`
access(all)
fun publicTypes(): [Type]
view fun publicTypes(): [Type]
}
```

Expand Down
4 changes: 2 additions & 2 deletions versioned_docs/version-1.0/language/accounts/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Any code can get a "read-only" reference to an account (`&Account`)
at a given address by using the built-in `getAccount` function:

```cadence
fun getAccount(_ address: Address): &Account
view fun getAccount(_ address: Address): &Account
```

### Performing write operations
Expand Down Expand Up @@ -131,7 +131,7 @@ but is not able to add another key to the account, for example.
Script can get any kind of access to any account, using the built-in `getAuthAccount` function:

```cadence
fun getAuthAccount<T: &Account>(_ address: Address): T
view fun getAuthAccount<T: &Account>(_ address: Address): T
```

This function is only available in scripts.
Expand Down
10 changes: 5 additions & 5 deletions versioned_docs/version-1.0/language/built-in-functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sidebar_position: 28
#

```cadence
fun panic(_ message: String): Never
view fun panic(_ message: String): Never
```

Terminates the program unconditionally
Expand All @@ -20,7 +20,7 @@ panic("something went wrong: ...")
## `assert`

```cadence
fun assert(_ condition: Bool, message: String)
view fun assert(_ condition: Bool, message: String)
```

Terminates the program if the given condition is false,
Expand All @@ -32,7 +32,7 @@ The message argument is optional.
## `revertibleRandom`

```cadence
fun revertibleRandom(): UInt64
view fun revertibleRandom(): UInt64
```

Returns a pseudo-random number.
Expand Down Expand Up @@ -79,7 +79,7 @@ Cadence provides RLP decoding functions in the built-in `RLP` contract, which do
### `decodeString`

```cadence
fun decodeString(_ input: [UInt8]): [UInt8]
view fun decodeString(_ input: [UInt8]): [UInt8]
```

Decodes an RLP-encoded byte array. RLP calls this a "string."
Expand All @@ -90,7 +90,7 @@ If the function encounters any error while decoding, the program aborts.
### `decodeList`

```cadence
fun decodeList(_ input: [UInt8]): [[UInt8]]`
view fun decodeList(_ input: [UInt8]): [[UInt8]]`
```

Decodes an RLP-encoded list into an array of RLP-encoded items.
Expand Down
12 changes: 8 additions & 4 deletions versioned_docs/version-1.0/language/crypto.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ access(all) enum HashAlgorithm: UInt8 {
access(all) case KECCAK_256 = 6

/// Returns the hash of the given data
access(all) fun hash(_ data: [UInt8]): [UInt8]
access(all) view fun hash(_ data: [UInt8]): [UInt8]

/// Returns the hash of the given data and tag
access(all) fun hashWithTag(_ data: [UInt8], tag: string): [UInt8]
access(all) view fun hashWithTag(_ data: [UInt8], tag: string): [UInt8]
}
```

Expand Down Expand Up @@ -138,6 +138,10 @@ struct PublicKey {
/// If called with any other signature algorithm, the program aborts
access(all)
view fun verifyPoP(_ proof: [UInt8]): Bool

// creating a PublicKey is a view operation
access(all)
view init()
}
```

Expand Down Expand Up @@ -283,7 +287,7 @@ The PoP can only be verified using the `PublicKey` method `verifyPoP`.
### BLS signature aggregation

```cadence
fun aggregateSignatures(_ signatures: [[UInt8]]): [UInt8]?
view fun aggregateSignatures(_ signatures: [[UInt8]]): [UInt8]?
```

Aggregates multiple BLS signatures into one.
Expand All @@ -300,7 +304,7 @@ verfiy multiple signatures of the same message.
### BLS public key aggregation

```cadence
fun aggregatePublicKeys(_ publicKeys: [PublicKey]): PublicKey?
view fun aggregatePublicKeys(_ publicKeys: [PublicKey]): PublicKey?
```

Aggregates multiple BLS public keys into one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ To get information about a block, the functions `getCurrentBlock` and `getBlock`

-
```cadence
fun getCurrentBlock(): Block
view fun getCurrentBlock(): Block
```

Returns the current block, i.e. the block which contains the currently executed transaction.

-
```cadence
fun getBlock(at: UInt64): Block?
view fun getBlock(at: UInt64): Block?
```

Returns the block at the given height.
Expand Down
26 changes: 13 additions & 13 deletions versioned_docs/version-1.0/language/run-time-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Type<Int>() == Type<Int>()
Type<Int>() != Type<String>()
```

The method `fun isSubtype(of: Type): Bool` can be used to compare the run-time types of values.
The method `view fun isSubtype(of: Type): Bool` can be used to compare the run-time types of values.

```cadence
Type<Int>().isSubtype(of: Type<Int>()) // true
Expand Down Expand Up @@ -61,7 +61,7 @@ type.identifier // is "A.0000000000000001.Test"

### Getting the Type from a Value

The method `fun getType(): Type` can be used to get the runtime type of a value.
The method `view fun getType(): Type` can be used to get the runtime type of a value.

```cadence
let something = "hello"
Expand Down Expand Up @@ -89,9 +89,9 @@ let type: Type = something.getType()
Run-time types can also be constructed from type identifier strings using built-in constructor functions.

```cadence
fun CompositeType(_ identifier: String): Type?
fun InterfaceType(_ identifier: String): Type?
fun IntersectionType(types: [String]): Type?
view fun CompositeType(_ identifier: String): Type?
view fun InterfaceType(_ identifier: String): Type?
view fun IntersectionType(types: [String]): Type?
```

Given a type identifier (or a list of identifiers for interfaces
Expand All @@ -115,20 +115,20 @@ let type2: Type = IntersectionType(
Other built-in functions will construct compound types from other run-types.

```cadence
fun OptionalType(_ type: Type): Type
fun VariableSizedArrayType(_ type: Type): Type
fun ConstantSizedArrayType(type: Type, size: Int): Type
fun FunctionType(parameters: [Type], return: Type): Type
view fun OptionalType(_ type: Type): Type
view fun VariableSizedArrayType(_ type: Type): Type
view fun ConstantSizedArrayType(type: Type, size: Int): Type
view fun FunctionType(parameters: [Type], return: Type): Type
// returns `nil` if `key` is not valid dictionary key type
fun DictionaryType(key: Type, value: Type): Type?
view fun DictionaryType(key: Type, value: Type): Type?
// returns `nil` if `type` is not a reference type
fun CapabilityType(_ type: Type): Type?
fun ReferenceType(entitlements: [String], type: Type): Type?
view fun CapabilityType(_ type: Type): Type?
view fun ReferenceType(entitlements: [String], type: Type): Type?
```

### Asserting the Type of a Value

The method `fun isInstance(_ type: Type): Bool` can be used to check if a value has a certain type,
The method `view fun isInstance(_ type: Type): Bool` can be used to check if a value has a certain type,
using the concrete run-time type, and considering subtyping rules,

```cadence
Expand Down
Loading