Stream: beginners

Topic: ✔ Destructure data types in host


view this post on Zulip Max (Aug 15 2024 at 21:47):

Hi,
I'm still trying to wrap my mind around writing platforms. One thing I really want to do is to require the roc main to not just return String, but, say, MyType : { x: U8, y: U8 }. As a rust beginner I was a bit blown away by the rust platform example, so I'm sticking to the c-platform for now. Naively I would want to just make a struct struct MyStruct { int x; int y }. Then change the mainForHost line a bit:

extern void roc__mainForHost_1_exposed_generic(struct MyStruct *string);

and then write in the main

struct MyCType r;
  roc__mainForHost_1_exposed_generic(&r);

  int x = r.x;
  int y = r.y;

  int sum = x + y;
  printf("%d + %d = %d\n", x, y, sum);

However this seems to be a bit too naive as it does not work :D Could someone maybe explain to me the basics of how to transport such data from roc?

view this post on Zulip Brendan Hansknecht (Aug 15 2024 at 21:51):

Most of the complexity here comes from Tasks. Without tasks, most of it is pretty direct like that.

view this post on Zulip Brendan Hansknecht (Aug 15 2024 at 21:52):

Have you just modified platform switching C?

view this post on Zulip Brendan Hansknecht (Aug 15 2024 at 21:52):

oh, though you are returning U8, which isn't int it is uint8_t or unsigned char

view this post on Zulip Brendan Hansknecht (Aug 15 2024 at 21:56):

This commit works for me to do what you asked: https://github.com/roc-lang/roc/commit/26d69bada770f40fded8b111a0f51be6882e8602

view this post on Zulip Max (Aug 15 2024 at 22:01):

You are right, I changed int to uint8_t in the c code and it works as expected now. So I wasn't that far off :D Thanks for the quick answer!

view this post on Zulip Notification Bot (Aug 21 2024 at 09:34):

Hannes has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC