Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 09, 2015 Providing custom formatting without importing half of phobos. | ||||
---|---|---|---|---|
| ||||
Let's say that I'm a library provider and I intend to offer a completely new data type suitable for printing with format() or writef(). According to this tutorial (http://wiki.dlang.org/Defining_custom_print_format_specifiers), to achieve this, one must import at least std.format : FormatSpec. The problem is that just importing FormatSpec will create exactly 88 phobos dependencies. Is there any way to provide formatting capabilities for a custom data type without importing the FormatSpec thing? |
November 09, 2015 Re: Providing custom formatting without importing half of phobos. | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Mon, Nov 09, 2015 at 03:09:26PM +0000, rumbu via Digitalmars-d-learn wrote: > Let's say that I'm a library provider and I intend to offer a > completely new data type suitable for printing with format() or > writef(). > > According to this tutorial (http://wiki.dlang.org/Defining_custom_print_format_specifiers), to achieve this, one must import at least std.format : FormatSpec. > > The problem is that just importing FormatSpec will create exactly 88 phobos dependencies. > > Is there any way to provide formatting capabilities for a custom data type without importing the FormatSpec thing? Do you need *custom* format specifiers? If all you need is to be able to write: format("%s", myData), then the only thing you need to do is to implement one of the toString methods recognized by format(), such as: void toString(scope void delegate(const(char)[]) dg) { ... } and format() should be able to just pick it up. T -- You have to expect the unexpected. -- RL |
November 09, 2015 Re: Providing custom formatting without importing half of phobos. | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Monday, 9 November 2015 at 16:11:23 UTC, H. S. Teoh wrote:
> On Mon, Nov 09, 2015 at 03:09:26PM +0000, rumbu via Digitalmars-d-learn wrote:
>> [...]
>
> Do you need *custom* format specifiers? If all you need is to be able to write: format("%s", myData), then the only thing you need to do is to implement one of the toString methods recognized by format(), such as:
>
> void toString(scope void delegate(const(char)[]) dg) {
> ...
> }
>
> and format() should be able to just pick it up.
>
>
> T
I'm working to create a decimal data type library, therefore I need at least %f %e %a and %g, including other data like width, precision and so on.
|
November 09, 2015 Re: Providing custom formatting without importing half of phobos. | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Monday, 9 November 2015 at 15:09:28 UTC, rumbu wrote:
> The problem is that just importing FormatSpec will create exactly 88 phobos dependencies.
I would pull request that to move it into its own independent module and then make std.format import that. I know that's not an immediate solution but it should go through by the next release; i doubt such a PR would be controversial.
|
Copyright © 1999-2021 by the D Language Foundation