py2many / py2many

Transpiler of Python to many other languages
MIT License
609 stars 46 forks source link

[a, b](bool) fails on Go, Kotlin and Nim #132

Open jayvdb opened 3 years ago

jayvdb commented 3 years ago

[a, b](bool) is a fairly common trick employed in Python, leveraging of the fact that False == 0 and True == 1

assert ["false", "true"][True] == "true"
assert ["false", "true"][False] == "false"

It works correctly in Rust and Julia (and C++ except for https://github.com/adsharma/py2many/issues/127), but not in Go, Kotlin and Nim.

Additionally, True > 0 & True == 0 fails except C++, Julia and Rust due to operator > not supporting bool, and the same for True == 1 and other comparison operators.

adsharma commented 3 years ago

https://github.com/golang/go/issues/9367 is related. We could be using:

https://golang.org/pkg/runtime/?m=all#bool2int

adsharma commented 3 years ago
  assert((std::vector<std::string>{std::string{"false"},
                                  std::string{"true"}})[int(true)] ==
         std::string{"true"});

works for cpp. Two fixes needed: