You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type `RichDocument` have been declared as a product type in the LambdaBuffers schema using the `prod` keyword.
115
115
116
-
In general, product types are mapped to [tuple types](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) in TypeScript most of the time. The exception is if there is only one element in the tuple in which case the type is translated to a type alias.
116
+
In general, product types are mapped to [tuple types](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) in Typescript most of the time. The exception is if there is only one element in the tuple in which case the type is translated to a type alias.
117
117
118
118
More precisely, given a LambdaBuffers' product type as follows
where the `...` denotes iterated `SomeTypei` for some `i`, then
125
125
126
-
- If `N = 0` so `prod MyProduct =`, then we map this to the TypeScript type
126
+
- If `N = 0` so `prod MyProduct =`, then we map this to the Typescript type
127
127
128
128
```ts
129
129
exporttypeMyProduct= []
130
130
```
131
131
132
-
- If `N = 1` so `prodMyProduct = SomeType1`, then we map this to the TypeScript type
132
+
- If `N = 1` so `prodMyProduct = SomeType1`, then we map this to the Typescript type
133
133
134
134
```ts
135
135
exporttypeMyProduct=SomeType1
136
136
```
137
137
138
138
i.e., `MyProduct` simply aliases `SomeType1`
139
139
140
-
- If `N >= 2` so `prodMyProduct = SomeType1 ... SomeTypeN`, then we map this to the TypeScript type
140
+
- If `N >= 2` so `prodMyProduct = SomeType1 ... SomeTypeN`, then we map this to the Typescript type
141
141
142
142
```ts
143
143
exporttypeMyProduct= [SomeType1, ..., SomeTypeN]
@@ -148,7 +148,7 @@ where the `...` denotes iterated `SomeTypei` for some `i`, then
148
148
## Sum types
149
149
The types `Author`, `Reviewer`, and `RichContent` have been declared as sum types in the LambdaBuffers schema using the `sum` keyword.
150
150
151
-
In general, sum types are mapped to a [union type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) in TypeScript and with the additional following rules.
151
+
In general, sum types are mapped to a [union type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) in Typescript and with the additional following rules.
152
152
Given a LambdaBuffers' sum type as follows
153
153
154
154
```purescript
@@ -160,13 +160,13 @@ sum MySum
160
160
161
161
where the `...` denotes either an iterated `Branchi` for some `i`, or an iterated `BranchiTypej` for some `i` and `j`, then each branch, say `Branchi` is translated as follows.
162
162
163
-
- If `Branchi` has no fields i.e., `|Branchi`, then the corresponding TypeScript type's union member is
163
+
- If `Branchi` has no fields i.e., `|Branchi`, then the corresponding Typescript type's union member is
164
164
165
165
```ts
166
166
| { name:'Branchi' }
167
167
```
168
168
169
-
- If `Branchi` has one or more fields i.e., `|BranchiBranchiType1 ... BranchiTypeMi`, then the corresponding TypeScript type's union member is
169
+
- If `Branchi` has one or more fields i.e., `|BranchiBranchiType1 ... BranchiTypeMi`, then the corresponding Typescript type's union member is
170
170
171
171
```ts
172
172
| { name:'Branchi'
@@ -176,7 +176,7 @@ where the `...` denotes either an iterated `Branchi` for some `i`, or an iterate
176
176
177
177
where `<ProducttranslationofBranchiType1 ... BranchiTypeMi>` denotes the right hand side of the [product translation](#product-types) of `prodFieldsProduct = BranchiType1 ... BranchiTypeMi`.
178
178
179
-
So, for example, given `|BranchiBranchiType1`, the corresponding TypeScript type is as follows
179
+
So, for example, given `|BranchiBranchiType1`, the corresponding Typescript type is as follows
180
180
181
181
```ts
182
182
| { name:'Branchi'
@@ -195,24 +195,41 @@ where the `...` denotes either an iterated `Branchi` for some `i`, or an iterate
195
195
## Record types
196
196
The types `Document` and `Chapter` have been declared as record types in the LambdaBuffers schema using the `record` keyword.
197
197
198
-
Record types are mapped to [object types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types) in TypeScript.
198
+
Record types are mapped to [object types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types) in Typescript.
TypeScript has no builtin implementation of type classes. As such, LambdaBuffers rolled its own type classes.
213
-
A complete usage example can be found in the [TypeScript Prelude sample project](./typescript-prelude/src/index.mts).
211
+
## Type classes quickstart
214
212
215
-
A type class in TypeScript is an object type which defines a set of methods.
213
+
Typescript has no builtin implementation of type classes. As such, LambdaBuffers rolled its own type classes.
214
+
A complete usage example can be found in the [Typescript Prelude sample project](./typescript-prelude/src/index.mts), but assuming the packaging is setup correctly, the interface to use a typeclass is as follows
In particular, we access a global variable `LbrPrelude.Eq` which contains the type class instances, and pick out a particular instance with the type's name like `LbrPrelude.Integer`. Note that the `LbrPrelude.Maybe` instance requires knowledge of the `Eq` instance of the `LbrPrelude.Integer`, so we must pass that in as a function argument.
229
+
230
+
## Type classes in detail
231
+
232
+
A type class in Typescript is an object type which defines a set of methods.
216
233
For example, the `Eq` type class in Haskell defines the set of methods `==` (equality) and `/=` (inequality) as follows.
217
234
218
235
```haskell
@@ -221,7 +238,7 @@ class Eq a where
221
238
(/=)::a->a->Bool
222
239
```
223
240
224
-
Thecorresponding [`Eq` class](https://github.com/mlabs-haskell/prelude-typescript/blob/main/src/Lib/Eq.ts) in TypeScript is:
241
+
The corresponding [`Eq` class](https://github.com/mlabs-haskell/prelude-typescript/blob/main/src/Lib/Eq.ts) inTypescript is:
225
242
226
243
```ts
227
244
export interface Eq<A> {
@@ -230,7 +247,7 @@ export interface Eq<A> {
230
247
}
231
248
```
232
249
233
-
Each type class in TypeScript must have an associated global variable which maps unique representations of its instance types to the corresponding object of the type class implementation.
250
+
Each type class in Typescript must have an associated global variable which maps unique representations of its instance types to the corresponding object of the type class implementation.
234
251
For example, the `Eq` type class has the [global variable](https://github.com/mlabs-haskell/lambda-buffers/blob/main/runtimes/typescript/lbr-prelude/src/LambdaBuffers/Eq.ts#L11) defined in the [lbr-prelude](https://github.com/mlabs-haskell/lambda-buffers/tree/main/runtimes/typescript/lbr-prelude) library defined as follows
235
252
236
253
```ts
@@ -276,7 +293,7 @@ instance (Eq a, Eq b) => Eq (MyPair a b) where
0 commit comments