Repository navigation
Description
There are many igraph functions that take enum constants, e.g. to choose a method or mode of operation to use.
The mapping from R to these values happens in:
https://github.com/igraph/rigraph/blob/main/tools/stimulus/types-RR.yaml
However, the R interface produces raw integers, and does not use the enum constant names in C. Here's an example:
%I% <- switch(igraph.match.arg(%I%), "cartesian"=0L, "lexicographic"=1L, "strong"=2L, "tensor"=3L)
Notice the values 0, 1, 2, 3 instead of the C constants IGRAPH_PRODUCT_CARTESIAN, IGRAPH_PRODUCT_LEXICOGRAPHIC, etc.
This situation is not ideal and somewhat dangerous. If we change the list of enum values in C, we do not technically break API (only ABI), but the raw integer encoding does change. This will break the R interface.
Luckily, this happens rarely, but there's a higher risk while working in C/igraph 1.0. It happened twice recently, namely:
- Removed a deprecated and no longer used value, to be released with 1.0. igraph/igraph@f8161f5
- Can happen accidentally when receiving external contributions, see e.g. https://github.com/igraph/igraph/pull/2772/files#r2099714867
Unfortunately I did not immediately realize that doing this breaks R. We'll have to review which enum values, if any, changed between 0.10 and 1.0. It may be only the single one in igraph_es_type_t. I'll try to do this and document such changes in the changelog, to make sure that you can pick it up on the R side. We'll also try to avoid such changes as much as possible.
But, if you have ideas on how to do it, it'd be best if we could find a solution to use the C enum names directly, and reduce the risk of breakage.
In the Mathematica interface I use a two-level solution:
- In Mathematica code, I translate strings to intermediate integers.
- Then in C code, I translate these intermediate integers to actual enum constant names.
This way the interface is robust to re-numbering the constants in C. Admittedly, this is a bit more work, but not by much, and can probably be done through types-RR.yaml (translates R strings to integers) and types-RC.yaml (translates intermediate integers to constant names).
CC @krlmlr
Activity
krlmlr commentedon Jul 7, 2025
How do you feel about keeping front-facing enum values stable for eternity?
szhorvat commentedon Jul 7, 2025
It's doable, but I'm not too happy about it. It increases the mental load when working with this: Yet another thing to keep in mind and pay attention to when reviewing PRs.
Here's an example where it's getting ugly. I really hate seeing this:
https://github.com/igraph/igraph/blob/009eb2f83d764af55acb01a6691d608abcf99a10/include/igraph_error.h#L274-L321
This one has to be kept in sync with an array of strings which got a lot of
NULLvalues as a result of the removed codes.At least for 1.0, I'm hoping to have a "clean start" and drop as much historical baggage as possible, instead of accumulating more.
On the other hand, the values MUST stay stable within a major release series (can't change between 1.x releases) to ensure ABI compatibility.
Ping @ntamas as well for input.
szhorvat commentedon Jul 7, 2025
Is this an issue for you in MATLAB-igraph, @DavidRConnell ?
What about in Julia, @Krastanov?
Krastanov commentedon Jul 7, 2025
On the julia side this is not a worry, all C enums are available by name, no need for remapping or using strings, e.g.:
A bit verbose because we do not export any of the symbols by default.
krlmlr commentedon Jul 7, 2025
For R, we might be able to save a bit of boilerplate by translating the C code that declares the enums into R. Do all constants fit into a global namespace, or are collisions to be expected?
szhorvat commentedon Jul 7, 2025
Do you mean the enum constant names in C? They must fit, as they share a namespace. Usually there's a prefix, as in
IGRAPH_PREFIX_SOMETHING.DavidRConnell commentedon Jul 7, 2025
Thanks for the warning. I try to avoid knowing the integer values of enums by using C-side arrays to map between enums and MATLAB-side strings with position defined by the enums. Like:
Then iterate over the array until I find a matching string:
There are a few places where I do depend on knowing the values though, namely the neighbor mode enum that starts at 1 instead of 0.
But I think I could rewrite this to not subtract out the minimum value and leave
modes[0]to its implicit value. Not sure if there's a dynamic way to determine the number of enum values though so I don't have to hardcode it inton_modes. In my internal enums I extra value at the end of the list to use for n.I don't know if that pattern would be useful to anyone else or if adding it in to igraph enums would break anything, but I find it convenient as the only order we have to assume is the
Nvalue will be the final member of the enum, if values are added in later the C compiler will handle all the changes.