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 constructor functions for run-time types #1195

Merged
merged 37 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a3a9d1a
add constructor for run-time optional type values
Oct 25, 2021
7477b6b
add constructor for run-time variable sized array type values
Oct 25, 2021
07be62a
Merge branch 'master' of github.com:onflow/cadence into issue1137
Oct 25, 2021
9235e28
add constructor for run-time constant-sized array type values
dsainati1 Oct 25, 2021
ccb3102
add constructor for run-time dictionary type values
dsainati1 Oct 25, 2021
4d38afd
add constructor for run-time composite type values
dsainati1 Oct 25, 2021
620d2af
add constructor for run-time interface values
dsainati1 Oct 25, 2021
7d079b3
add constructor for run-time function values
dsainati1 Oct 25, 2021
37b31db
gofmt
dsainati1 Oct 26, 2021
1e56cf4
fix data race
dsainati1 Oct 26, 2021
e95d150
add constructor for run-time reference values
dsainati1 Oct 26, 2021
62f374c
type checking for runtime constructor for restricted types
dsainati1 Oct 26, 2021
a250336
factor restricted type checking out of conversion to make it usable i…
dsainati1 Oct 26, 2021
53ccd71
added runtime type constructor for capability types
dsainati1 Oct 26, 2021
dcfdd57
added more tests
dsainati1 Oct 26, 2021
87d3c43
interpreter implementation for runtime construction of restricted types
dsainati1 Oct 26, 2021
1fee847
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Oct 26, 2021
5bdbe4e
fix lint
dsainati1 Oct 27, 2021
b39fc81
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Oct 29, 2021
8b13613
respond to review comments
dsainati1 Oct 29, 2021
f8f8bd9
added documentation
dsainati1 Oct 29, 2021
35d75c8
respond to review
dsainati1 Nov 1, 2021
2016411
refactor interpreter
dsainati1 Nov 1, 2021
2a71f90
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Nov 1, 2021
6e9325f
Apply suggestions from code review
dsainati1 Nov 1, 2021
5d5deba
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Nov 2, 2021
b048055
Merge branch 'issue1137' of github.com:onflow/cadence into issue1137
dsainati1 Nov 2, 2021
5521190
fix lint
dsainati1 Nov 2, 2021
26229db
Apply suggestions from code review
dsainati1 Nov 3, 2021
8309494
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Nov 3, 2021
94ffbc1
Merge branch 'issue1137' of github.com:onflow/cadence into issue1137
dsainati1 Nov 3, 2021
4866a14
lint
dsainati1 Nov 3, 2021
4dd4148
add test for builtin composite types
dsainati1 Nov 3, 2021
e8b2db4
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Nov 4, 2021
a83cd3f
respond to review
dsainati1 Nov 4, 2021
a86f1ab
Merge branch 'master' of github.com:onflow/cadence into issue1137
dsainati1 Nov 5, 2021
d14cb20
rename error
dsainati1 Nov 5, 2021
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
43 changes: 43 additions & 0 deletions docs/language/run-time-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ let type: Type = something.getType()
// `type` is `Type<@Collectible>()`
```

### Constructing a Run-time Type

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 RestrictedType(identifier: String?, restrictions: [String]): Type?
```

Given a type identifer (as well as a list of identifiers for restricting interfaces
in the case of `RestrictedType`), these functions will look up nominal types and
produce their run-time equivalents. If the provided identifiers do not correspond
to any types, or (in the case of `RestrictedType`) the provided combination of
identifiers would not type-check statically, these functions will produce `nil`.

```cadence
struct Test {}
struct interface I {}
let type: Type = CompositeType("A.0000000000000001.Test")
// `type` is `Type<Test>`

let type2: Type = RestrictedType(
identifier: type.identifier,
restrictions: ["A.0000000000000001.I"]
)
// `type2` is `Type<Test{I}>`
```

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
// returns `nil` if `key` is not valid dictionary key type
fun DictionaryType(key: Type, value: Type): Type?
// returns `nil` if `type` is not a reference type
fun CapabilityType(_ type: Type): Type?
fun ReferenceType(authorized: bool, 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,
Expand Down
13 changes: 13 additions & 0 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,16 @@ func (e NonStorableStaticTypeError) Error() string {
e.Type,
)
}

// InterfaceMissingLocation is reported during interface lookup,
// if an interface is looked up without a location
type InterfaceMissingLocation struct {
QualifiedIdentifier string
}

func (e *InterfaceMissingLocation) Error() string {
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(
"tried to look up interface %s without a location",
e.QualifiedIdentifier,
)
}
Loading