-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation for intersection types (#167)
* remove pub, priv and pub(set) documentation * fix accidentally updated unrelated files * fix accidentally updated unrelated files * start updating * revert non-cadence doc changes * add documentation for attachments * entitlement documentation * more detail for entitlement mappings * add notes about references * update best practices * remaining auth uses * Apply suggestions from code review Co-authored-by: Joshua Hannan <[email protected]> * and clarifying details * Update docs/cadence/language/references.mdx * Update docs/cadence/language/references.mdx * Apply suggestions from code review * docs for intersection types --------- Co-authored-by: Joshua Hannan <[email protected]> Co-authored-by: Alex <[email protected]>
- Loading branch information
1 parent
4a21588
commit b6d1364
Showing
13 changed files
with
117 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
title: Intersection Types | ||
sidebar_position: 16 | ||
--- | ||
|
||
Interface types cannot be referenced directly; | ||
instead they must be used as part of intersection types. | ||
An intersection type represents a value that conforms to all of the interfaces listed in the intersection. | ||
|
||
The syntax of a intersection type is `{U1, U2, ... Un}`, | ||
where the types `U1` to `Un` are the interfaces that the type conforms to. | ||
|
||
The members and functions of any of the set of interfaces are available. | ||
|
||
Intersection types are useful for writing functions that work on a variety of different inputs. | ||
For example, by using an intersection type for a parameter's type, | ||
the function may accept any concrete value that implements all the interfaces in that intersection. | ||
The value is restricted to the functionality of the intersection; | ||
if the function accidentally attempts to access other functionality, | ||
this is prevented by the static checker. | ||
|
||
```cadence | ||
access(all) struct interface HasID { | ||
access(all) let id: String | ||
} | ||
access(all) struct A: HasID { | ||
access(all) let id: String | ||
init(id: String) { | ||
self.id = id | ||
} | ||
} | ||
access(all) struct B: HasID { | ||
access(all) let id: String | ||
init(id: String) { | ||
self.id = id | ||
} | ||
} | ||
// Create two instances, one of type `A`, and one of type `B`. | ||
// Both types conform to interface `HasID`, so the structs can be assigned | ||
// to variables with type `{HasID}`: Some resource type which only allows | ||
// access to the functionality of resource interface `HasID` | ||
let hasID1: {HasID} = A(id: "1") | ||
let hasID2: {HasID} = B(id: "2") | ||
// Declare a function named `getID` which has one parameter with type `{HasID}`. | ||
// The type `{HasID}` is a short-hand for `AnyStruct{HasID}`: | ||
// Some structure which only allows access to the functionality of interface `HasID`. | ||
// | ||
access(all) fun getID(_ value: {HasID}): String { | ||
return value.id | ||
} | ||
let id1 = getID(hasID1) | ||
// `id1` is "1" | ||
let id2 = getID(hasID2) | ||
// `id2` is "2" | ||
``` | ||
|
||
If more than two interfaces are present in an intersection type, | ||
any concrete value of that type must implement both of them: | ||
|
||
```cadence | ||
access(all) struct interface HasMetadata { | ||
access(all) let metadata: AnyStruct | ||
} | ||
access(all) struct C: HasID, HasMetadata { | ||
access(all) let id: String | ||
access(all) var metadata: AnyStruct | ||
init(id: String) { | ||
self.id = id | ||
} | ||
fun setMetadata(_ data: AnyStruct) { | ||
self.metadata = data | ||
} | ||
} | ||
// valid, because `C` implements both `HasID` and `HasMetadata`. | ||
let hasID3: {HasID, HasMetadata} = C(id: "3") | ||
// valid, because `A` implements only `HasID`. | ||
let hasID4: {HasID, HasMetadata} = A(id: "4") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b6d1364
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
docs-smoky-iota.vercel.app
docs-onflow.vercel.app
docs-git-main-onflow.vercel.app
developers.flow.com