radon-project / radon

The Radon Programming Language
https://radon-project.github.io
GNU General Public License v3.0
23 stars 2 forks source link

[Feature]: A Second Argument to `pyapi()` #103

Closed angelcaru closed 2 months ago

angelcaru commented 2 months ago

Is your feature request related to a problem? Please describe. When using pyapi(), often the only way to get what you want is using string interpolation. But this is very error-prone.

Describe the solution you'd like The second (optional) argument to pyapi() would be a namespace where to execute the code. This would also get rid of the ugly output variable. Example:

ns = {}
ns["my_var"] = 69
pyapi("
assert isinstance(my_var, int) # Radon values converted to native Python values. Special care would be needed in the case of functions.
assert my_var == 69
my_output = 'abc'
", ns)
assert ns["my_output"] == "abc" # Python values are converted back to Radon values. Once again, special care would be needed in the case of functions.

Describe alternatives you've considered I also considered making the second argument be anything, which you could access through a magical input variable (similar to output). But you would often end up with the same pattern of passing a List or HashMap and pulling the values out. It's just better to do that directly

Almas-Ali commented 2 months ago

Total disaster when converting Python >--< Radon objects! Every trial is crashing!

Almas-Ali commented 2 months ago

Can you give it a try?

angelcaru commented 2 months ago

Yeah, sure

Almas-Ali commented 2 months ago

Thanks!

angelcaru commented 2 months ago

Not done yet, but I'm getting somewhere...

Almas-Ali commented 2 months ago

I think we need two functions, which will map all nested objects and convert to Radon to Python and Python to Radon.

angelcaru commented 2 months ago

That's exactly what I'm doing!

Almas-Ali commented 2 months ago

Great! Hopefully this will work.

angelcaru commented 2 months ago

I got it working, but one of the test cases is failing :( Hopefully I'll be able to fix it

angelcaru commented 2 months ago

I fixed it :)