Replies: 3 comments 4 replies
-
I think it depends on if you are trying to support a general feature, or just get something to work. To get something working like this with minimal changes to the compiler I would probably create a global array, or a local value that you expose to other functions via an effect handler. |
Beta Was this translation helpful? Give feedback.
-
How about something like this:
// stack-allocated.c
typedef struct {
ssize_t size;
uint32_t* data;
} kk_array;
kk_box_t kk_with_array_1024(kk_function_t f, kk_context_t* ctx) {
kk_array arr;
uint32_t data[1024];
arr.size = 0;
arr.data = &data[0];
return kk_function_call(kk_box_t,(kk_function_t,intptr_t,kk_context_t*),f,(f,(intptr_t)&arr,ctx),ctx);
}
kk_std_core_types__maybe kk_array_get(kk_array* arr, ssize_t i, kk_context_t* ctx) {
if (i >= arr->size) {
return kk_std_core_types__new_Nothing(ctx);
}
return kk_std_core_types__new_Just(kk_int32_box(arr->data[i], ctx), ctx);
}
kk_std_core_types__maybe kk_array_set(kk_array* arr, ssize_t i, uint32_t x, kk_context_t* ctx) {
if (i >= 1024) {
return kk_std_core_types__new_Nothing(ctx);
}
arr->data[i] = x;
arr->size = i+1;
return kk_std_core_types__new_Just(kk_unit_box(kk_Unit), ctx);
} |
Beta Was this translation helpful? Give feedback.
-
The stack is allocated in I have a wrapper struct in Koka that has is uses a 'scoped' type wrapper around the internal pointer to help prevent escaping of the array into the surrounding context. Additionally I would make sure you keep the internal pointer field private, and only export a public api using the I've used a similar pattern a lot in my libuv PR #384, and I hope that we get some better built-in support for strongly typed c pointers using #494. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm a newbie to Koka and functional programming in general. I'm trying to create an array datatype allocated on stack with C as the backend. I'm looking for suggestions on how to achieve this.
Ideally it should look very similar to vector in Koka. However, allocating the array and passing it around as a pointer with size is little challenging.
#define array(type, name, size) type name[size];
Some of the thoughts in my mind are:
struct
is generated from Koka. However, I was not successful in achieving this as instruct
we try to create a datatype/class-like and in array I'm trying to create a variable/value.main
function which uses this functionality.@TimWhiting Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions