This commit is contained in:
Tony Garnock-Jones 2022-02-11 22:51:04 +01:00
parent 3ef314bc21
commit 4c61507658
1 changed files with 9 additions and 5 deletions

View File

@ -208,12 +208,15 @@ for the type definitions above is the following:
```preserves ```preserves
version 1 . version 1 .
BookOutline = { BookOutline = {
"sections": @sections [BookItem ...], "sections": @sections [BookItem ...],
} . } .
BookItem = @chapter { "Chapter": @value Chapter } BookItem = @chapter { "Chapter": @value Chapter }
/ @separator "Separator" / @separator "Separator"
/ @partTitle { "PartTitle": @value string } . / @partTitle { "PartTitle": @value string } .
Chapter = { Chapter = {
"name": @name string, "name": @name string,
"sub_items": @sub_items [BookItem ...], "sub_items": @sub_items [BookItem ...],
@ -242,11 +245,13 @@ Using the TypeScript schema compiler, we see
```typescript ```typescript
export type BookOutline = {"sections": Array<BookItem>}; export type BookOutline = {"sections": Array<BookItem>};
export type BookItem = ( export type BookItem = (
{"_variant": "chapter", "value": Chapter} | {"_variant": "chapter", "value": Chapter} |
{"_variant": "separator"} | {"_variant": "separator"} |
{"_variant": "partTitle", "value": string} {"_variant": "partTitle", "value": string}
); );
export type Chapter = {"name": string, "sub_items": Array<BookItem>}; export type Chapter = {"name": string, "sub_items": Array<BookItem>};
``` ```
@ -255,9 +260,9 @@ Using the Racket schema compiler, we see
```racket ```racket
(struct BookOutline (sections)) (struct BookOutline (sections))
(define (BookItem? p) (define (BookItem? p)
(or (BookItem-chapter? p) (or (BookItem-chapter? p)
(BookItem-separator? p) (BookItem-separator? p)
(BookItem-partTitle? p))) (BookItem-partTitle? p)))
(struct BookItem-chapter (value)) (struct BookItem-chapter (value))
(struct BookItem-separator ()) (struct BookItem-separator ())
(struct BookItem-partTitle (value)) (struct BookItem-partTitle (value))
@ -281,8 +286,7 @@ BookItem = Chapter / =separator / @partTitle string .
Chapter = <chapter @name string @sub_items [BookItem ...]> . Chapter = <chapter @name string @sub_items [BookItem ...]> .
``` ```
The schema compilers produce **exactly the same** type definitions for this variation! The schema compilers produce **exactly the same** type definitions for this variation.
The differences are in the (de)serialization code only. The differences are in the (de)serialization code only.
Here's the Preserves value equivalent to the example above, expressed using the Preserves-native schema: Here's the Preserves value equivalent to the example above, expressed using the Preserves-native schema: