ID:2759859
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
__COUNTER__ macro would allow or new heights of macro abuse, a lot of compilers already implement such macro, as it is pretty straight forward, every time it is encountered it is replaced with a number that is increased.

Proposed behaviour:
#define TO_STRING(x) #x
#define ACCESS_FOO(x) f.foo##x

/datum/foobar
var/foo3 = "example"
var/foo4 = "bar"

/proc/main()
world.log << TO_STRING(__COUNTER__)
world.log << TO_STRING(__COUNTER__)
world.log << TO_STRING(__COUNTER__)
var/datum/foobar/f = new()
world.log << ACCESS_FOO(__COUNTER__)
world.log << ACCESS_FOO(__COUNTER__)

Proposed Output:
0
1
2
example
bar


For easier control i'd suggest adding a passable value it that would be the 'id' of the counter to be used, by default using counter id '0' like so
Proposed Extension to the previous idea:
#define TO_STRING(x) #x

/proc/main()
world.log << TO_STRING(__COUNTER__) //using counter id 0
world.log << TO_STRING(__COUNTER__(1)) //explicity using counter id 1
world.log << TO_STRING(__COUNTER__(0)) //explicity using counter id 0
world.log << TO_STRING(__COUNTER__) // using id 0 by default

Proposed Output:

0
0
1
2




I'm not sure if this would be usable inside another #define macro, and the function and non-function forms might be iffy, but otherwise I guess it probably wouldn't be an issue.

But what would the use case be?
Well my specific use case for it is using it as a sort of UUID (I'm abusing the hell out of macros to simulate interface-like relationships between objects) currents my macro looks somewhat like this

#define IMPLEMENTS(UUID, object path, interface path) some code


by using __COUNTER__ i could strip that UUID argument from the macro making life of the end-users of my work just that tiny bit easier.
Could also be used to simplify defining bitflags, though ideally it would also be possible to reset the counter to use it for this purpose.