Island importing C Api: Enum import issue

I have a C type enum imported into Island.

  • Why the imported Island Enum has all those duplicate elements?
  • The imported Enum.ToString doesn’t work, with the compiler complaining “(E44) No member “ToString” on type "TF_Code

How can I get ToString to work, so I can convert an Enum value to string?

typedef enum TF_Code {
TF_OK = 0,
TF_CANCELLED = 1,
TF_UNKNOWN = 2,
TF_INVALID_ARGUMENT = 3,
TF_DEADLINE_EXCEEDED = 4,
TF_NOT_FOUND = 5,
TF_ALREADY_EXISTS = 6,
TF_PERMISSION_DENIED = 7,
TF_UNAUTHENTICATED = 16,
TF_RESOURCE_EXHAUSTED = 8,
TF_FAILED_PRECONDITION = 9,
TF_ABORTED = 10,
TF_OUT_OF_RANGE = 11,
TF_UNIMPLEMENTED = 12,
TF_INTERNAL = 13,
TF_UNAVAILABLE = 14,
TF_DATA_LOSS = 15,
} TF_Code;

The above C enum was imported as:

TensorFlow.TF_Code = public enum (
TF_OK = 0, OK = 0,
TF_CANCELLED = 1, CANCELLED = 1,
TF_UNKNOWN = 2, UNKNOWN = 2,
TF_INVALID_ARGUMENT = 3, INVALID_ARGUMENT = 3,
TF_DEADLINE_EXCEEDED = 4, DEADLINE_EXCEEDED = 4,
TF_NOT_FOUND = 5, NOT_FOUND = 5,
TF_ALREADY_EXISTS = 6, ALREADY_EXISTS = 6,
TF_PERMISSION_DENIED = 7, PERMISSION_DENIED = 7,
TF_UNAUTHENTICATED = 16, UNAUTHENTICATED = 16,
TF_RESOURCE_EXHAUSTED = 8, RESOURCE_EXHAUSTED = 8,
TF_FAILED_PRECONDITION = 9, FAILED_PRECONDITION = 9,
TF_ABORTED = 10, ABORTED = 10,
TF_OUT_OF_RANGE = 11, OUT_OF_RANGE = 11,
TF_UNIMPLEMENTED = 12, UNIMPLEMENTED = 12,
TF_INTERNAL = 13, INTERNAL = 13,
TF_UNAVAILABLE = 14, UNAVAILABLE = 14,
TF_DATA_LOSS = 15, DATA_LOSS = 15);

It’s a trick the importer does to make the enum nicer to use. They allhave a TF_ prefix so it strips that. However it of course keeps the original so any code that uses that will still work.

@ck
this imported enum doesn’t support ToString ?

Is there a way to have ToString working? That would make enum value to name conversion easier.

Yeah the problem is that it’s a “foreign enum”. And foreign enums don’t have metadata at all. Which is why it doesn’t have ToString. (Normally, there’s a whole VMT for structs/enums defined in Elements, among of which there’s RTTI, but we can’t create this for foreign enums as there’s no actual code from our end for using a foreign library).

@ck

Got it. Thank you.

Would it be possible to have this meta info auto created for Enum/Record type when imported?

That would be very helpful for importing a C lib with a lot of Enum/Record definitions, and easier to build higher level abstractions on top of that.

At some point we did this actually, on first use, copy it into the using module. However this caused issues because you ended up with two copies of the exact same type if two modules used it, causing linking issues.

I think your best bet for this is an aspect, for now.