Nim: how can I make it closer to Python syntax?

365 views Asked by At

I know it's generally NOT a good idea but I want to make Nim more "Pythonic". Examples:

1) instead of proc, use def

2) instead of echo, use print

3) instead of readLine, use input

4) instead of parseJson use json.loads

and so on.

Yes, it may not be possible to change the behavior of the functions and statements, but I'd like it to - at least - look like the "good old" Python ones.

Honestly, please don't explain me why you think it's a bad idea. I want to play and try it. No animals would be harmed, blah-blah.

Any ideas?

Thank you!

1

There are 1 answers

0
bluenote10 On

For echo, readLine, and parseJson you can look up their definitions in system.nim and json.nim and define your own procs. This should work:

import json

proc print*(x: varargs[expr, `$`]) {.magic: "Echo", tags: [WriteIOEffect], sideEffect.}

proc input*(f: File): TaintedString  {.tags: [ReadIOEffect], benign.}

proc loads(p: var JsonParser): JsonNode = parseJson(p)

Regarding def, I don't think it is possible with exactly the same syntax as for proc. If you want to, you can come up with some def macro, which itself generates the AST of some proc. But as far as I can see, the resulting syntax for defining a proc would be pretty ugly.