Hello, Nim community!
I'm working on macros which allows usage of C++ libraries from C backend with minimal effort. During reflection on the idea I've encountered some questions:
when isCppBackend:
... # use typical importcpp pragma
when isJSBackend:
... # error message
when isCBackend:
... # use some magic to represent C++ objects in C terms
The part of the idea realization (without C backend support) can be found here
See this example where I give an error on JS backend:
when defined(js):
{.fatal: "linalg is only available for native backends".}
I assume there should be similar flags for cpp and so on, but I am not sure where to find the exact list (there should be also Objective C and PHP?)
Thank you, andrea!
It really works! I've made tests and found which keywords work.
when defined(js):
echo "Hello JS!"
when defined(c):
echo "Hello C!"
when defined(cpp):
echo "Hello C++!"
when defined(objc):
echo "Hello ObjC!"
when defined(php):
echo "Hello PHP!"
nim c --run test.nim
nim cpp --run test.nim
Hello C++!
nim objc --run test.nim
Hello ObjC!
nim js test.nim # Hello JS!
nim php --run test.nim
Hello JS!
As it can be seen, only cpp, js and objc keywords are correct. JS checker also works with php target.
I don't know if such constant is already defined (seems not), but you could use something like this:
type Backend {.pure.} = enum
C, CPP, OBJC, JS, PHP
const backend = when defined(cpp): Backend.CPP
elif defined(objc): Backend.OBJC
elif defined(nimphp): Backend.PHP
elif defined(js): Backend.JS
else: Backend.C
It is a bit tough to manage this as a user, so I think it would make sense to add such constant to system lib.