On Tuesday, 30 March 2021 at 13:28:55 UTC, novice3 wrote:
Hello.

When i adapt C code, i see new type creation:
  typedef void* Xobj;

Or code like this:
  struct _Xobj;
  typedef struct _Xobj *Xobj;


I want create derived type in D, found std.typecons.Typedef template, and write:
  alias Xobj = Typedef!(void*, (void*).init);

But compiler use long type name in error messages, like this:

Error: function test6.foo(Typedef!(void*, null, null) obj) is not callable using argument types (void*)

cannot pass argument bad of type void* to parameter Typedef!(void*, null, null) obj

This messages dont help me understand, which type should i use.
What i should change?
Or Typedef template should be changes?
Any Typedef alternatives?

The typedef in C in D is just an alias:

```
alias Xobj = void*;
```

Xobj can then be used interchangeably with void*, so all void* arguments accept Xobj and all Xobj arguments accept void*.

If you want a type-safe alias that makes all void* arguments accept Xobj but not Xobj arguments to accept void* you can use `Typedef` like you linked. However using this language built-in feature is much simpler and cheaper in terms of resource usage and compile time + always results in the fastest code: (there is no conversions at all)

```
enum Xobj : void*;
```

This allows explicit conversion in both ways using cast, but only allows implicit conversion from Xobj to void*, not from void* to Xobj:

```
void* x = someValue;
Xobj y = cast(Xobj)x; // ok
x = y; // ok
y = x; // error (need explicit cast)
```

Reply via email to