Hello.
I need subtype uint to store ipv4 address.
It should be like ordinary uint,
except conversion to string with %s format.

My try  https://run.dlang.io/is/fwTc0H  failed on last assert:

```
struct IpV4Address
{
  private uint ip;
  alias ip this;

  string toString()
  {
    import std.conv: to;
    return to!string((ip >>> 24) & 0xFF) ~ "." ~
           to!string((ip >>> 16) & 0xFF) ~ "." ~
           to!string((ip >>> 8) & 0xFF) ~ "." ~
           to!string(ip & 0xFF);
  }
}
void main()
{
  import std.format: format;
  IpV4Address x;
  x = 0x01020304;  // 1.2.3.4
  assert( x ==  0x01020304 );
  assert( format("%s", x) == "1.2.3.4" );
  assert( format("%x", x) == "01020304" );  // FAILED
  /+
std.format.FormatException@/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(4065):
 Expected '%s' format specifier for type 'IpV4Address'
  +/
}
```

Is my goal (subtype should be normal uint, but with pretty to-string conversion) possible?

Thanks.

Reply via email to