Zig: is there a way to know the name of an identifier

Created on 11 Jan 2020  路  11Comments  路  Source: ziglang/zig

I want to print the name of a variable or a function.

question

Most helpful comment

Zig already has this. Put double quotes around the variable name.

All 11 comments

Can you provide an example of what you mean?

Zig has:

@errorName
@memberName
@tagName
@typeName

Example:

var a = 1;
warn("{} = {}", .{@varName(a), a});

So, something like @varName, @fnName and @moduleName would be useful.
Or universal @name.

@daurnimator something like @data-man example

But... all that does is literally emit its argument as a string. Replace @varName(a) with "a" and this does exactly what you want it to. I don't see the use in a builtin for this.

Replace @varname(a) with "a"

Strings are not checked by the compiler when refactoring. The builtin would be more helpful.

I'm afraid I don't see the point. Are there any good use cases of this?

@pixelherodev if zig has something like the example, we don't have to do it manually.

For use-cases, judging by how the C# nameof built-in that was introduced a few years ago is mostly used, mostly revolves around diagnostics code-paths (log outputs, in-production OpenTracing spans, debug-time printfs etc) and certain code-gen scenarios (incl. not just C#-code gen but whatever freakish query-langs or custom templated / config artifacts etc). And "ArgumentException" as in runtime / test-time errors for bad args. Yes, varName would probably be much rarer "needed" than memberName / typeName usages overall. Stably "important" items usually add up to structs and thus memberNames fairly quickly. But the above things come to mind.

Zig already has this. Put double quotes around the variable name.

Maybe a new ticket that is more specific should be opened? Some of the chat here and in Discord thought that closing this issue meant Zig wouldn't support introspect on function/parameter/struct/field names, etc, and I don't think that's the case.

That aside, I think there's some value to not just putting it in quotes, even in the code example given above.

V1:

var a = 1;
warn("{} = {}", .{@varName(a), a});

V2:

var a = 1;
warn("{} = {}", .{"a", a});

If you do an IDE refactor-rename on a, then the string will get out of sync with the variable name in V2, but not in V1.

I might buy the argument that people would probably not even bother to find out that @varName() existed if they were just doing throw-away debug prints. However it might still be useful for logging code that was written to be more maintainable and robust? Also, maybe there's other use cases I haven't considered?

Either way, I think there's at least tiny bit of value to this feature, even if it is low priority.

Edit: I think this is basically what @kristoff-it meant, at least how I read it, but maybe spelling it out in its entirety helps make it more clear :)

Yes, I was just pointing out what the common use is of this type of functionality, or at least how I've seen it being used in C#.

To be honest I'm not a big fan of it, I just wanted to clarify the concept a little bit to make sure people don't miss the point.

Anyway, Andrew seems to be even less of a fan than I am.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewrk picture andrewrk  路  3Comments

bronze1man picture bronze1man  路  3Comments

andersfr picture andersfr  路  3Comments

dobkeratops picture dobkeratops  路  3Comments

fengb picture fengb  路  3Comments