Jump to page: 1 2 3
Thread overview
Wanted: Format character for source code literal
Apr 30
Berni44
Apr 30
Berni44
May 01
Berni44
May 04
Berni44
May 05
Berni44
May 06
Berni44
May 06
Berni44
May 08
Berni44
April 30

I plan to add an extension to std.format, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:

enum a = <something>;
enum b = mixin(format!"%S"(a));

static assert(a == b && is(typeof(a) == typeof(b)));

(Please note, that even for floats a == b should hold for all values, but NaNs; I plan to use RYU for this.)

The big question is now, which character to use. I thought of %S like source code literal. Andrei suggested %D like D literal. Both ideas have the disadvantage of using uppercase letters, which would break the of uppercase letters meaning that the output uses uppercase instead of lowercase (i.e. 1E10 instead of 1e10).

A first idea of a lowercase literal might be %l but this might easily be confused with %I and %1 (both don't exist); and also l is used in C's printf for long which we luckily don't need here. Anyway I fear confusion.

What do you think? Which letter would be best?

April 30

On Friday, 30 April 2021 at 07:10:38 UTC, Berni44 wrote:

>

I plan to add an extension to std.format, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:

[...]

Actually S is already used. The only specifier that is "free" would be D. But, what's so bad with using L? 🤔

April 30

On Friday, 30 April 2021 at 07:20:11 UTC, Imperatorn wrote:

>

Actually S is already used.

Huh, where? Not in std.format...

April 30
On Friday, 30 April 2021 at 07:10:38 UTC, Berni44 wrote:
> I plan to add an extension to `std.format`, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:
>
> [...]

%m in reference to mixin ?
April 30

On Friday, 30 April 2021 at 08:02:44 UTC, Berni44 wrote:

>

On Friday, 30 April 2021 at 07:20:11 UTC, Imperatorn wrote:

>

Actually S is already used.

Huh, where? Not in std.format...

No I meant for [w]printf

https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions

April 30
On 4/30/21 4:10 AM, Patrick Schluter wrote:
> On Friday, 30 April 2021 at 07:10:38 UTC, Berni44 wrote:
>> I plan to add an extension to `std.format`, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:
>>
>> [...]
> 
> %m in reference to mixin ?

I like this.

I also could be OK with capital D. I know it normally stands for make uppercase, but decimal numbers have no letters in them.

-Steve
April 30
On Fri, Apr 30, 2021 at 07:10:38AM +0000, Berni44 via Digitalmars-d wrote:
> I plan to add an extension to `std.format`, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:
> 
> ```
> enum a = <something>;
> enum b = mixin(format!"%S"(a));
> 
> static assert(a == b && is(typeof(a) == typeof(b)));
> ```
[...]

What's the scope of this feature? Can <something> be, say, a code literal like a lambda? Can std.format even support printing the function body of a lambda in a way that can be parsed by mixin()?  How far do we intend to go with this?  Or does this only apply to POD values?

I can imagine there'd be problems if you have, say, a class from a different module with private members, possibly with nested private classes, so you couldn't actually reconstruct the class instance from a string alone.

Unless the scope is significantly constrained, I see the potential for this feature devolving into a big time-sink that really only caters to a niche use-case. I'd be happy to be proven wrong, though.


T

-- 
"You know, maybe we don't *need* enemies." "Yeah, best friends are about all I can take." -- Calvin & Hobbes
May 01

On Friday, 30 April 2021 at 07:10:38 UTC, Berni44 wrote:

>

I plan to add an extension to std.format, namely a new format character with the meaning of producing a source code literal. Or more formally, the following snippet should work for every type this extension will support:

[...]

If this would be unique to mixins, then %m, if not, L(iteral) or D(literal).

May 01

On Friday, 30 April 2021 at 21:11:55 UTC, H. S. Teoh wrote:

>

What's the scope of this feature? Can <something> be, say, a code literal like a lambda? Can std.format even support printing the function body of a lambda in a way that can be parsed by mixin()? How far do we intend to go with this? Or does this only apply to POD values?

I can imagine there'd be problems if you have, say, a class from a different module with private members, possibly with nested private classes, so you couldn't actually reconstruct the class instance from a string alone.

Unless the scope is significantly constrained, I see the potential for this feature devolving into a big time-sink that really only caters to a niche use-case. I'd be happy to be proven wrong, though.

Of course, there are limits to this. I intend to implement it for bools, integers, floats, characters, strings and enums. I think it will also be possible to implement it for (associative) arrays, as long as the elements can be implemented like this. Structs, classes, interfaces and unions should implement it in their toString functions. I wouldn't go so far to provide generic code for them. With that we'll indeed run into problems. (Maybe with the exception of structs with default constructor.) I haven't made up my mind about pointers yet.

And well functions, delegates and lambdas? Functions aren't supported by std.format at all and the implementation of delegates is broken and useless and should be removed (in my opinion).

May 01

On Friday, 30 April 2021 at 07:10:38 UTC, Berni44 wrote:

>

I plan to add an extension to std.format, namely a new format character with the meaning of producing a source code literal.

Would %q work? In reference to token strings:

enum a = <something>;
enum b = mixin(format!"%q"(a));
enum c = mixin(q{<something>});

static assert(a == b && is(typeof(a) == typeof(b)));
static assert(c == b && is(typeof(c) == typeof(b)));

— Bastiaan.

« First   ‹ Prev
1 2 3