konchunas / pyrs

Python to Rust transpiler
MIT License
523 stars 33 forks source link

Using generics instead of type inference on structs and functions #2

Open ghost opened 5 years ago

ghost commented 5 years ago

Instead of full blown type inference (which will be VERY hard to do because of python's loose, dynamic typing), why not make the resulting code generate generics, and then halt the transpiling if it sees type changing?

I can help implement. If you need, let me know.

konchunas commented 5 years ago

I sure wouldn't mind a help. You see, currently transpiler is already generates generics for method arguments. It's half way there for structs too. But the main problem here is type bounds. So if you have a generic type argument and do, for example, math operations on it, this type should be bound by where clause. You can check it by simple python example:

def foo(bar):
  bar = bar - 5;

This will make bar to be generic type T0, but compiler will say that op::Sub is not implemented for T0 That's the main problem

ghost commented 5 years ago

@konchunas What I would then suggest is doing some kind of parsing on whatever the variables are assigned to initially. If the variable is reassigned, parse it again and if it fails to parse or produces a different output, you know the types changed. If you do any kind of integer operations or see that the type is an integer, automatically implement the required trait to the function.

konchunas commented 5 years ago

When dealing with this transpiler you don't deal with script evaluation, we more deal with Syntax Tree. But you can take a look at this call to get a grasp on what I think is the idiomatic way to search in Syntax Tree.

You could investigate if it is difficult to search for variable assignments inside some scope (e.g. function body) using similar method.