www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ImportC with typedef anonymous enums

reply rmc <rjmcguire gmail.com> writes:
Could we make enums work the way structs do in importC?

For example (example2d.d):
```
import example2;

void main() {
	my_struct param;
	my_enum eopt;
	//param.opts = A; // not okay
	someFunc(param);
	someFunc2(param.opts); // okay

	someFunc2(eopt); // not okay
}
```
Error is:
```
example2d.d(10): Error: function `example2.someFunc2(__tag3 
evalue)` is not callable using argument types `(__anonymous)`
example2d.d(10):        cannot pass argument `eopt` of type 
`__anonymous` to parameter `__tag3 evalue`
```

example2.c:
```
int printf(const char *format, ...);

typedef enum {
	A = 1,
	B = 1 << 1,
} my_enum;

typedef struct {
	int i;
	my_enum opts;
} my_struct;


void someFunc(my_struct s) {
	printf("okay\n");
}

void someFunc2(my_enum evalue) {
	printf("okay2\n");

}


// void main() {
// 	my_struct param;
// 	my_enum eopt;
// 	param.opts = A; // okay
// 	someFunc(param);
// 	someFunc2(param.opts); // okay

// 	someFunc2(eopt); // okay
// }
```
Nov 09 2022
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 9 November 2022 at 17:48:14 UTC, rmc wrote:
 Could we make enums work the way structs do in importC?

 For example (example2d.d):
 ```
 import example2;

 [...]
Just a sidenote, but for some reason, casting it like this works: someFunc2((cast(typeof(my_struct.opts))eopt));
Nov 09 2022
parent reply Chance Snow <git chancesnow.me> writes:
I am also experiencing this issue with the following C enum:

```c
// A list of all possible value types in WebAssembly.
typedef enum {
   // Signed 32 bit integer.
   I32,
   // Signed 64 bit integer.
   I64,
   // Floating point 32 bit integer.
   F32,
   // Floating point 64 bit integer.
   F64,
   // A 128 bit number.
   V128,
   // A reference to a Wasm function.
   FuncRef,
   // A reference to opaque data in the Wasm instance.
   ExternRef,
} ExtismValType;
```

On Wednesday, 9 November 2022 at 18:20:18 UTC, Imperatorn wrote:
 Just a sidenote, but for some reason, casting it like this 
 works:
 someFunc2((cast(typeof(my_struct.opts))eopt));
Not sure why, either, but this workaround also works for me.
Aug 31 2023
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 1 September 2023 at 02:10:15 UTC, Chance Snow wrote:
 I am also experiencing this issue with the following C enum:

 ```c
 // A list of all possible value types in WebAssembly.
 typedef enum {
   // Signed 32 bit integer.
   I32,
   // Signed 64 bit integer.
   I64,
   // Floating point 32 bit integer.
   F32,
   // Floating point 64 bit integer.
   F64,
   // A 128 bit number.
   V128,
   // A reference to a Wasm function.
   FuncRef,
   // A reference to opaque data in the Wasm instance.
   ExternRef,
 } ExtismValType;
 ```

 On Wednesday, 9 November 2022 at 18:20:18 UTC, Imperatorn wrote:
 Just a sidenote, but for some reason, casting it like this 
 works:
 someFunc2((cast(typeof(my_struct.opts))eopt));
Not sure why, either, but this workaround also works for me.
Are you in the discord server btw?
Sep 01 2023