-
Notifications
You must be signed in to change notification settings - Fork 18
Update EncodeDecode example for new Roc static dispatch #251
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
base: main
Are you sure you want to change the base?
Conversation
- Replace old abilities-based encoding with new static dispatch - Demonstrate where clauses for encode_str and encode_list - Show custom type encoding with Person example - Update README with new syntax and expected output Note: This example uses new Roc syntax and will work once the examples repo is updated to new Roc.
Anton-4
left a comment
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.
Thanks @codegod100 :)
I would like to split this up into two examples:
- Minimal encode-decode example: Easily digestible for human brains.
- Larger, more advanced encode-decode example
Maybe we should wait with updating till we have more auto derive stuff implemented in the new compiler? That will change the code a lot.
- We should also update this to a notebook file format once that is implemented in the new compiler. That makes it easy to ensure all code in the .md file stays up-to-date.
examples/EncodeDecode/main.roc
Outdated
| # Define a Person type that can be encoded as a JSON object | ||
| Person := [Person({ name : Str, age : U64 })].{ | ||
| # Custom encode method for Person - encodes as JSON object | ||
| encode : Person, JsonFormat -> List(U8) | ||
| encode = |self, fmt| { | ||
| # Get the inner record via pattern match | ||
| match self { | ||
| Person({ name, age }) => { | ||
| # Encode name as JSON string | ||
| name_bytes = name.encode(fmt) | ||
|
|
||
| # Encode age as number (no quotes) | ||
| age_bytes = Str.to_utf8(age.to_str()) | ||
|
|
||
| # Build: {"name":"...","age":...} | ||
| var $result = Str.to_utf8("{\"name\":") | ||
| $result = $result.concat(name_bytes) | ||
| $result = $result.concat(Str.to_utf8(",\"age\":")) | ||
| $result = $result.concat(age_bytes) | ||
| $result = $result.concat(Str.to_utf8("}")) | ||
| $result | ||
| } | ||
| } | ||
| } | ||
| } |
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.
I believe this will be more auto derived in the future, can you add a TODO comment above this to make this use auto derive when it is implemented?
This PR updates the EncodeDecode example to use the new Roc static dispatch syntax.
Changes
encode_strandencode_listmethod requirementsPersonexample showing nested encodingNote
This example uses new Roc syntax (static dispatch,
match,var, etc.) and will work once the examples repo is updated to new Roc.Based on: roc-lang/roc@22cf61f
cc @Anton-4 - migrated from roc-lang/basic-cli#415 per your suggestion