Is this useful for others? Is it worth adding in a PR?
My goal was to understand the platform types better.
For example if I run roc glue JsonGlue.roc test-glue/ main.roc
on the following, i get the below JSON. Note I had to add Encoding
ability to the Types
which was an extremely minor addition.
platform "test"
requires {} { main : Foo }
exposes []
packages {}
imports []
provides [mainForHost]
Foo := [Number U8, String Str]
mainForHost : Foo
mainForHost = main
Generates types.json
{
"aligns": [1, 1, 8, 8],
"deps": [],
"entrypoints": [{ "T": ["mainForHost", 3] }],
"sizes": [1, 1, 24, 32],
"target": {
"architecture": { "X86x64": [] },
"operatingSystem": { "Mac": [] }
},
"types": [
{ "Unit": [] },
{ "Num": [{ "U8": [] }] },
{ "RocStr": [] },
{
"TagUnion": [
{
"NonRecursive": [
{
"discriminantOffset": 24,
"discriminantSize": 1,
"name": "Foo",
"tags": [
{
"name": "Number",
"payload": { "Some": [1] }
},
{ "name": "String", "payload": { "Some": [2] } }
]
}
]
}
]
}
],
"typesByName": [{ "T": ["Foo", 3] }]
}
I tested Inspect
out too, which works well, but I just wanted a format that I could read in my editor and easily format with newlines etc.
app "json-glue"
packages {
pf: "../platform/main.roc",
json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
}
imports [
pf.Types.{ Types },
pf.File.{ File },
json.Core.{json},
]
provides [makeGlue] to pf
makeGlue : List Types -> Result (List File) Str
makeGlue = \typesByArch ->
typesByArch
|> List.map convertTypesToFile
|> List.concat staticFiles
|> Ok
staticFiles : List File
staticFiles = []
convertTypesToFile : Types -> File
convertTypesToFile = \types ->
content =
Encode.toBytes types json
|> Str.fromUtf8
|> Result.withDefault "INVALID UTF8 ENCODED FROM JSON"
{ name: "types.json", content }
Perhaps a good fit for the examples repo?
Given it's a super small change, and this might help others. I'll make a quick PR to add the Encoding, and Inspect abilities to the Types so others can do this without changing the compiler.
Ok PR is https://github.com/roc-lang/roc/pull/6670
Last updated: Jul 06 2025 at 12:14 UTC