Compile Scala to .mpy?

Has anyone attempted to compile Scala code to .mpy?

(I did find Scalapy, https://scalapy.dev/, but I want to do the inverse . That project looks good but is thwarting my search.)

Thanks,

David

I donā€™t know of such a project, but what you could try, is a two-step approach: Use ScalaJS to compile Scala to JavaScript, then convert that to Python, i.e. using Js2Py.

1 Like

Do you want to access Java from Python or convert Scala code to Python code? The latter seems implausible.

For the first case for standard Python look at py4j.
For your ā€œversionā€ of Python I donā€™t know if it is possible.

I also suspect that because you want to work on embedded systems https://www.jython.org/ will also not work for you. But even if it did you still have to be aware that not all Python libraries may be accessed this way. @ansvonwa proposed solution may also have this problem.

HTHs

Interesting ā€“ why? I can see that it might be an infeasible amount of work to do well, but weā€™ve got enough proof-of-concept cross-compiles at this point that it seems possible. And the proposed two-step approach seems like a worthwhile experiment, although limited in what it could accomplish.

1 Like

@jducoeur The problem is that its not just a question of mapping the simple language constructs like if and while, but you need to:

  • Convert programs constructs that donā€™t exist in the other language;
  • Adapt the data structures that are not available in the ā€œhostā€ language

I addition to this, the OP seems to want to use this on an embedded system. Which means you now have to cater for hardware restriction and most probably donā€™t have OS support.

I would say it would be pretty difficult to get this going. :cold_sweat:

Now that I think of it, if the embedded system uses Linux maybe Native Scala could cut it?

1 Like

The equivalent of ScalaJS, but for Python, wouldnā€™t be any easier or harder than JS I would imagine? Which is to say that it is not impossible, but just a whole lot of work. Of course, a big part of this work ā€“ namely, rewriting a lot of the Java stdlib in pure scala so it can be compiled, has already been done.

I would absolutely love to see such a project, so that Scala can stay relevant in the machine learning/data science world that increasingly relies on Python. But itā€™s definitely a large project.

Thanks all.

Iā€™d hoped to find a project that already did most of the work - because of its value to the data science community. It looks like they mostly work the other way - running a JVM alongside python - with a few people running python within the JVM.

Iā€™m working with students using Lego robots. Python isnā€™t a good first language for 10-year-olds, and they get frustrated with Scratchā€™s lack of abstractions.

The newer system - Spike Prime - runs the micropython OS ( https://micropython.org/ ) which is a lot less to support; some things from the full JDK just wouldnā€™t make sense. I donā€™t think Scala Native would be viable there, but a similar approach could work.

(I originally tried getting Scala Native running on the older EV3 system, but Scala Native baked in 64-bit and the ARM cpu in the EV3 is 32-bit. I have had a lot of success - very good performance - using Scala on ev3dev OSā€™s 32-bit JVM.)

Maybe supporting micropython as an initial - smaller than everything - step would make sense, but I think itā€™s beyond what I can contribute to.

1 Like

It seems like Scala Native has 32-bit support in the pipe, by judging from:

The problem (for me) with waiting for Scala native 32-bit is ā€¦ the waiting. Itā€™ll be great when it is available, but the kids get older every year, and - now that Lego has stopped making the EV3 - the EV3s get older, more broken, and more expensive. I plan to use it when itā€™s available if I still have a working EV3 and kids willing to learn it. Itā€™s a slow train coming.

Spike Prime - and micropython - are contemporary technologies in 2022, even if they are more limited.

For Lego robotics I wouldnā€™t need all of Java supported. The project is nearly pure Scala, with just a little device access via nio. On Spike Prime the device access is available from python; Iā€™d just need some way to generate the right python to access the devices.

After a little digging - it looks like I could call compiled C code from the micropython OS. And could (with 32-bit support) compile Scala native code for that.

Has anyone called compiled Scala native (in its current 64-bit form) from python? That could address a broad front of problems.

Someone posted about this topic in r/scala (Reddit). I donā€™t know if this person succeeded but it might be worth to look for this thread.

I found a few things in progress in Scala Native. Itā€™s not possible yet, but is work in progress.

See Allow to link as dynamic library by WojciechMazur Ā· Pull Request #2145 Ā· scala-native/scala-native Ā· GitHub - for Scala Native to produce .o files that can be called from inside Python.

And Add support for 32-bit platforms through a linker-level switch by shadaj Ā· Pull Request #1571 Ā· scala-native/scala-native Ā· GitHub for 32-bit Scala Native.