How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Oct 9 17:52:58 UTC 2023


On Monday, October 9, 2023 10:55:41 AM MDT rempas via Digitalmars-d-learn 
wrote:
> On Monday, 9 October 2023 at 16:53:55 UTC, mw wrote:
> > but you `import std.stdio;`?
> >
> > Or copy the std/conv.d over to your build,
> >
> > or copy / write a toString(int) function yourself, which is
> > compile-time callable.
>
> I do on that example just to use "write". It wouldn't be
> necessary, but I just included it. My normal project does not use
> Phobos.

The language does not have a way to convert variables to strings. The
normal solution for that is to use Phobos.

The closest to a built-in solution would be toString on classes and structs,
but structs don't necessarily have a toString. Rather, in many cases, Phobos
does introspection on the struct to figure out how to convert the struct's
members to a string if no toString is present (which isn't necessarily
pretty but generally works well for debug output). Anyone looking to be able
to do that sort of thing without Phobos is either going to need to
reimplement what Phobos does themselves or use a third party library that
already does that.

std.conv.to provides the ability to convert between types in general based
on their constructors, opCast member functions, and toString (if the
user-defined type has one). So, it's able to convert pretty much any type to
a string using to!string.

std.format.format is then D's equivalent to snprintf, and it's able to
convert pretty much any type to a string. I'm not sure if it uses to!string
internally though.

Regardless, if you want to be able to convert an arbitrary variable to a
string without using Phobos, you basically have to reimplement all of that
yourself (or at least as much as you need to do whatever it is that you're
doing). C functions like snprintf can be used to convert the primitive types
(meaning that you wouldn't have to implement something like the mess that is
converting floating points to string), but for user-defined types that don't
have toString, you'd basically be forced to do the kind of type
introspection that Phobos does to produce a string from each of the member
variables.

It's possible that there's library somewhere that doesn't rely on Phobos
that provides some of the same functionality (e.g. for the guys looking to
use -betterC as a long term solution rather than just as a porting tool),
but the language itself doesn't have that kind of functionality.

Now, if what you're looking to do is to specifically convert an integer to a
string at compile time (which means that snprintf wouldn't be a solution,
since you can't call C functions during CTFE) rather than converting
variables in general to string, then it shouldn't be hard to write a simple
function that converts from an integer to a string. So, if that's all that
you're looking to do, it shouldn't be hard to avoid Phobos. However, you're
still going to need to implement it yourself.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list