What's up guys,
I'm trying to wrap a C library and there's a struct with a field name of type that needs be wrapped in Nim.
Of course I get a compile-time error when trying to use the type keyword so I encased it in backticks, but it still throws an error.
What is usually done in instances like this? Unless I'm still doing something wrong encasing the type field name with backticks doesn't seem to be working.
If I need to show the C struct and how I'm currently wrapping/using it I will, let me know!
Does your field not look something like this?
type Foo = object
`type`: int
If this doesn't work, I believe this should:
type Foo = object
typeField {.importc: "type".}: int
for the first case, yes, why not
type Foo = object
`type`: int
var f = Foo(`type`: 10)
echo f
C Library struct:
typedef struct lxw_conditional_format {
uint8_t type;
uint8_t criteria;
double value;
lxw_format *format;
}
Nim object:
lxw_conditional_format* = ref object
`type`* :lxw_conditional_format_types
criteria* :lxw_conditional_criteria
value* :cdouble
format* :lxw_formatPtr
# C function below
# lxw_error worksheet_conditional_format_range(lxw_worksheet *worksheet,
# lxw_row_t first_row,
# lxw_col_t first_col,
# lxw_row_t last_row,
# lxw_col_t last_col,
# lxw_conditional_format
# *conditional_format);
proc conditional_format_range*(worksheet :lxw_worksheetPtr, first_row :lxw_row_t, first_col :lxw_col_t, last_row :lxw_row_t, last_col :lxw_col_t, conditional_format :ptr lxw_conditional_format) :lxw_error {.importc:"worksheet_conditional_format_range",discardable.}
The C struct says uint8 for the type and criteria fields, but it takes an enum that corresponds to those numbers.
My implementation:
var redFmt = workbook.add_format()
redFmt.set_bg_color(0xFFC7CE)
redFmt.set_fg_color(0x9C0006)
var redCFmt = lxw_conditional_format()
redCFmt.kind = LXW_CONDITIONAL_TYPE_CELL
redCFmt.criteria = LXW_CONDITIONAL_CRITERIA_LESS_THAN
redCFmt.value = 0
redCFmt.format = redFmt
echo outputSheet.conditional_format_range(12,16,
19,18,redCFmt.addr)
I get an error called "parameter validation error", also saying "invalid type value".
Thank you, I didn't know about this ability:
type Foo = object
typeField {.importc: "type".}: int
I'm going to try a few things and report back here. If this doesn't work then it has to be something else and I'm absolutely oblivious to it.How are you able to access the field after declaring it like that?
I guess that typeField is its name in Nim's universe, thus, access it as foo.typeField.
I feel I should delete this post out of shame, but it was a learning experience. I found out that I needed to manually allocate memory and store the object there- I had no clue!
I had to watch videos on C to understand some concepts I found in a random example (like using calloc()- I was like wtf is that??) and then translating this into Nim somehow. A lot of research!
Took me a few hours but at least now I know how to use create() and resize() and where to use it in SOME cases when interfacing with C lol
Thanks for the help guys
No, you just made public what everyone needs to learn with C FFI (for Nim or any other language).
Now if a charitable soul can turn this in a tutorial, everyone benefits ;).