Package: pyala
pyalaPython 3.10 to Scala 2.13 TranspilerWarningProject under development. Not available right now. Come back later.TutorialHow to use pyalaDefine your python function and add type annotations to its input parameters.Declare new variables with empty annotated assignment to specify their local scope.Use one ofpyala.to_str,pyala.to_objectorpyala.to_fileto transpile your code.frompyalaimportpyala# define your functionsdeffoo():...defgoo():...# get the source code of a single functionprint(pyala.to_str(foo))# get the source code of multiple functions bundled in an objectprint(pyala.to_object(foo,goo,object_name='Example'))# save the source code in a fileprint(pyala.to_file(foo,goo,filepath='/tmp/Example.scala'))Examplesdeffoo():y:intifx<0:y=x**2else:y=x*2returny+xDiscrepanciesHere are a list of considerations to take into account due to discrepancies between scala and python.ImportsImports are not supported except for:import mathThe modulescala.mathis automatically imported and will mimic python built-in functions related to math, and the librarymathitself. This means that this code is valid, even though it uses an import:importmathdeffoo(x:float):returnmath.sqrt(x)Reference typesA python reference cannot change type because scala does not allow it.For instance, this python code is invalid in scala:x=4x=Truebecause x is defined as a scala.Integer and cannot change to scala.Boolean.Local ScopeIn python, you can return a variable defined in a local scope. This is not allowed in scala.For instance, this is valid python code:deffoo():ifTrue:x=5returnxbut this is not valid is scala:deffoo()={if(true){valx=5}x}However, you can access variable defined in a parent scope, like this:deffoo()={varx:Integer=0if(true){x=5}x}For this reason, pyala requires that you add an annotated assignment for each variable that you want to create:deffoo():x:int=0ifTrue:x=5returnxVariable function argumentsYou cannot assign a new value to a function argument. This code is invalid:deffoo(x:int):x=x+1returnxYou must create a new variable:deffoo(x:int):z=x+1returnzF-stringString interpolation (f-string) is typed in scala. This means that this code is invalid:deffoo(x:int):returnf"{x:0.2f}"You can only using float with "f" format:deffoo(x:int):z:float=float(x)returnf"{z:0.2f}"*args and **kwargsFirst,**kwargsare not supported. Second,*argsmust always comes last in the argument list. This means that this code:deffoo(*x:int,z:int=0):returnsum(x)-zis transpiled to:deffoo(z:Int=0,x:Int*)={(x.sum-z)}Note the order of the parameters is z then x instead of x then z.You can call this function correctly in python withgoo(3,4,5,6,z=7)but it will give a compilation error in scala because the correct call isgoo(7,3,4,5,6).Be extra careful when calling function defined in this way. The order will be correct in python but not in scala.operatorFloorDivThe return of floor div will always be a scala.Long, even if you use float.Eg.:
In python: 3.0//2 -> 1.0 (float)
In scala: 3.0//2 -> 1 (Long)hexIn python,hex(-42)returns"-0x2a", in scala it returns"0xffffffd6".ReferencesPython AST:https://docs.python.org/3/library/ast.htmlBuilt-in Functions:https://docs.python.org/3/library/functions.htmlMath library:https://docs.python.org/3/library/math.htmlNot Supportedexpr NamedExprLambdaLambda expressions are not supported:lambdax:x+1operator MatMulfor loop with continueasyncThe keywordasyncis not supportedcomplex