Compile time int to string conversion in BetterC

bauss jacobbauss at gmail.com
Fri Aug 19 10:22:25 UTC 2022


On Thursday, 18 August 2022 at 22:00:06 UTC, Paul Backus wrote:
> On Wednesday, 17 August 2022 at 11:38:31 UTC, Steven 
> Schveighoffer wrote:
>> On 8/17/22 6:38 AM, Dennis wrote:
>>> On Wednesday, 17 August 2022 at 08:44:30 UTC, Ogi wrote:
>>>> Maybe I’m missing something?
>>> 
>>> I had the same problem, and came up with the following trick:
>>> 
>>> ```D
>>> enum itoa(int i) = i.stringof;
>>> ```
>>
>> I have the same thing in my code:
>>
>> ```d
>> enum intStr(int x) = x.stringof;
>> ```
>
> I merged a version of this into Phobos (for internal use):
>
> https://github.com/dlang/phobos/blob/v2.100.1/std/conv.d#L5986-L5987
>
> It also comes with a unit test, so we don't get surprise 
> breakage if the behavior of `.stringof` ever changes.

Is there a reason why .stringof is implementation defined and not 
clearly defined in the spec how types and declarations should be 
treated when being "converted to a string"?

I find it really odd that it's implementation defined and you 
essentially can't rely on it anywhere.

It's something that has baffled me a lot.

Like are there something I'm missing that means it cannot be 
specified?


More information about the Digitalmars-d-learn mailing list