logoalt Hacker News

lou1306today at 9:23 AM2 repliesview on HN

Oof that's not a good look. You can trivially avoid this by just prefixing all variable names with a salt. By the way this (or a variant thereof) is called "avoiding unwanted name capture" if you want to sound all sophisticated :)


Replies

ventanatoday at 5:14 PM

I think maybe the idea of all these transpilers is to keep the generated code readable and as close to the original code as possible. Mangling names into unreadable garbage is not difficult, but that would also make the the transpiler act more like an obfuscator, which is probably what the authors want to avoid.

Protobuf has similar problem: whatever names you use for your proto messages should be translated to the target language as is, and with multiple target languages, chances are that you'll hit a keyword in one of them; each language plugin should figure out how to resolve this.

  $ cat test.proto
  syntax = "proto3";
  
  package test;
  
  message TestMessage {
    int32 register = 1;
    int32 foo = 2;
  }
  
  $ protoc --cpp_out=. test.proto
  
  $ grep 'register\|foo' test.pb.cc | head -n 2
        : register__{0},
          foo_{0},
(I may understand `register__`, but why `foo_`? no idea)
bruce343434today at 2:55 PM

C++ compilers do this too, thats why so many to-be-linked symbols start with "Z"

show 1 reply