@@ -1002,6 +1002,69 @@ union-cases ::= ty
1002
1002
| ty ',' union-cases?
1003
1003
```
1004
1004
1005
+ ### Item: ` resource `
1006
+
1007
+ A ` resource ` statement defines a new abstract type for a * resource* , which is
1008
+ an entity with a lifetime that can only be passed around indirectly via [ handle
1009
+ values] ( #handles ) . Resource types are used in interfaces to describe things
1010
+ that can't or shouldn't be copied by value.
1011
+
1012
+ For example, the following Wit defines a resource type and a function that
1013
+ takes and returns a handle to a ` blob ` :
1014
+ ``` wit
1015
+ resource blob
1016
+ transform: func(blob) -> blob
1017
+ ```
1018
+
1019
+ As syntactic sugar, resource statements can also declare any number of
1020
+ * methods* , which are functions that implicitly take a ` self ` parameter that is
1021
+ a handle. A resource statement can also contain any number of * static
1022
+ functions* , which do not have an implicit ` self ` parameter but are meant to be
1023
+ lexically nested in the scope of the resource type. Lastly, a resource
1024
+ statement can contain at most one * constructor* function, which is syntactic
1025
+ sugar for a function returning a handle of the containing resource type.
1026
+
1027
+ For example, the following resource definition:
1028
+ ``` wit
1029
+ resource blob {
1030
+ constructor(init: list<u8>)
1031
+ write: func(bytes: list<u8>)
1032
+ read: func(n: u32) -> list<u8>
1033
+ merge: static func(lhs: borrow<blob>, rhs: borrow<blob>) -> blob
1034
+ }
1035
+ ```
1036
+ desugars into:
1037
+ ``` wit
1038
+ resource blob
1039
+ %[constructor]blob: func(self: borrow<blob>, bytes: list<u8>) -> blob
1040
+ %[method]blob.write: func(self: borrow<blob>, bytes: list<u8>)
1041
+ %[method]blob.read: func(self: borrow<blob>, n: u32) -> list<u8>
1042
+ %[static]blob.merge: func(lhs: borrow<blob>, rhs: borrow<blob>) -> blob
1043
+ ```
1044
+ These ` % ` -prefixed [ ` name ` s] ( Explainer.md ) embed the resource type name so that
1045
+ bindings generators can generate idiomatic syntax for the target language or
1046
+ (for languages like C) fall back to an appropriately-prefixed free function
1047
+ name.
1048
+
1049
+ When a resource type name is used directly (e.g. when ` blob ` is used as the
1050
+ return value of the constructor above), it stands for an "owning" handle
1051
+ that will call the resource's destructor when dropped. When a resource
1052
+ type name is wrapped with ` borrow<...> ` , it stands for a "borrowed" handle
1053
+ that will * not* call the destructor when dropped. As shown above, methods
1054
+ always desugar to a borrowed self parameter whereas constructors always
1055
+ desugar to an owned return value.
1056
+
1057
+ Specifically, the syntax for a ` resource ` definition is:
1058
+ ``` ebnf
1059
+ resource-item ::= 'resource' id resource-methods?
1060
+ resource-methods ::= '{' resource-method* '}'
1061
+ resource-method ::= func-item
1062
+ | id ':' 'static' func-type
1063
+ | 'constructor' param-list
1064
+ ```
1065
+
1066
+ The syntax for handle types is presented [ below] ( #handles ) .
1067
+
1005
1068
## Types
1006
1069
1007
1070
As mentioned previously the intention of ` wit ` is to allow defining types
0 commit comments