Skip to content

Type inference in the style of Py14 #10

@MaD70

Description

@MaD70

Hello,

Interesting project! While skimming your documentation, I came across this section in Type hints:

You must use Python-style type hints everywhere. I.e., for variable definitions, function parameters, class data members, and return types.

This reminded me of another Python-to-C++ (14) transpiler, Py14 (the project was archived in 2018), where this restriction is lifted via type inference. In practice, this means introducing type variables through C++ templates and using the auto keyword (and decltype also).

Here's an example from the project README to illustrate how this is implemented:

Example

Original Python version:

def factorial(num):
    if num <= 1:
        return num
    return factorial(num - 1) * num

Transpiled C++ template:

template <typename T1> auto factorial(T1 num) {
    if (num <= 1) {
        return num;
    }
    return factorial(num - 1) * num;
}

While I find an explicitly typed version of the code clearer, it might be interesting to support transpilation of code without type annotations as well—both to speed up the porting of existing Python programs and to enable rapid prototyping. This could complement your plan to introduce type variables, if you haven’t already implemented it.

P.S.: I came across this after reading the thread on r/Compilers on Reddit (I don’t have an account), and I find the naysayers’ comments rather discouraging—they completely missed the potential of this project. Honestly, some of the remarks also seemed quite lacking in technical understanding. If I may, I’d encourage you not to let that discourage you.

P.P.S.: this isn’t really a feature request (which is why I didn’t add that label); it’s more of a suggestion to take a look at that abandoned project and, if appropriate, draw some inspiration from it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions