I would like to use these constants to define a "framebuffer" inline array
struct FrameBuffer {
private let pixels: [(CONFIG_LED_PANEL_WIDTH * CONFIG_LED_PANEL_HEIGHT) of AbstractColor]
}
However, I cannot do this, because integer #define macros are CInt aka Int32 and not Int
~/Developer/esp/projects/lightbox/components/rendering/src/swift/Pattern.swift:5:48: error: cannot convert value of type 'Int32' to raw type 'Int'
3 |
4 | struct FrameBuffer {
5 | private let pixels: [(CONFIG_LED_PANEL_WIDTH * CONFIG_LED_PANEL_HEIGHT) of AbstractColor]
| `- error: cannot convert value of type 'Int32' to raw type 'Int'
6 | }
7 |
While the integer literal expressions in general do currently support all the different flavors of Int types, integer generic parameters don't yet support anything except the native Swift Int type... Not sure if disallowing implicit Int32 -> Int conversion in integer literal expressions was explicitly disallowed / intentional (the implicit conversion is definitely not allowed in normal Swift expressions), but the plumbing of accepting #define constants is indeed there at least. If the #define constant is a long, it does seem to be imported as an Int and is accepted in literal expressions for me! Try it out for yourself by writing your constant like this: #define MY_VALUE 80L.
Unfortunately this sort of constant type mismatch isn't even something you can easily fix for a C library using Clang API notes though... API notes can only replace a C global’s type with an ABI-compatible type, so changing a 32bit C int to 64bit Swift Int gives an error when I tried: "API notes replacement type long has a different size from original type const int"
Thanks for the tip! unfortunately Kconfig doesn't allow me to specify the constants as a long, so I don't see a way to achieve what I was going for the moment with LiteralExpressions enabled.
Generic parameters of integer types other than Int
We discuss generalizing value generic parameters to types other than Int as a future direction above, but a narrower expansion might be to allow all of Swift's primitive integer types, including all of the sized and signed/unsigned variants, as types for generic value parameters. One could argue that UInt is particular is desirable to use as the type for fixed-size and fixed-capacity collections, of which instances can never actually be constructed for negative sizes.
However, we would like to continue to promote the use of Int as the common currency type for integers, as we have already established for the standard library. Introducing mixed integer types as type-level generic parameters would inevitably lead to the need to be able to perform type conversions at type level, and the associated need to deal with overflow during these type-level conversions.
You can define your own little C header file that reexports the #define constants as static const intptr_t and that seems to import fine into Swift for me:
Good shout! I plopped those in my BridgingHeader.h rather than defining a whole module but it works just the same :) I agree that it's not ideal, though...