templated toString and class inheritance

cc cc at nevernet.com
Sun Dec 25 19:14:58 UTC 2022


On Sunday, 25 December 2022 at 19:10:13 UTC, cc wrote:
> Fortunately, `std.format` provides a 
> non-[necessarily-]allocating alternative:
>
> ```d
> import std.format;
> import std.range.primitives; // Mandatory, see aside
> ```

Aside: importing `std.range.primitives` is necessary to use the 
templated toString version, but the compiler error for failing to 
do so is *highly* unintuitive as to this simple requirement:
```
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format\internal\write.d(2440): Error: template instance `std.format.internal.write.formatObject!(LockingTextWriter, Animal, char)` does not match template declaration `formatObject(Writer, T, Char)(ref Writer w, ref T val, ref scope const FormatSpec!Char f)`
   with `Writer = LockingTextWriter,

        T = test.Animal,

        Char = char`

   whose parameters have the following constraints:

   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

`  > hasToString!(T, Char)

`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
   Tip: not satisfied constraints are marked with `>`
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format\write.d(1239): 
Error: template instance 
`std.format.internal.write.formatValueImpl!(LockingTextWriter, 
Animal, char)` error instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format\write.d(632):   
      instantiated from here: `formatValue!(LockingTextWriter, 
Animal, char)`
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1710):        
instantiated from here: `formattedWrite!(LockingTextWriter, char, 
Animal)`
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(4227):        
instantiated from here: `write!(Animal, char)`
```



More information about the Digitalmars-d mailing list