Skip to content

[BUG] No way to declare std::function that use references in cpp2 #343

Closed
@filipsajdak

Description

@filipsajdak

In the current implementation of cppfront (5fa2707) in cpp2, there is no way to declare std::function that uses references.

The following examples failed to work:

f1: () -> std::function<std:string(std::string const&)> = { // error: missing = before function body (at '<')
    return :(s : std::string) -> std::string = {
        return s + " World!";
    };
}
f2: () -> std::function<(std::string) -> std::string> = { // error: missing = before function body (at '<')
    return :(s : std::string) -> std::string = {
        return s + " World!";
    };
}
f3: () -> std::function<std::string(std::string)> = {
    return :(s : std::string) -> std::string = {
        return s + " World!";
    };
}

The last example passes cppfront, but generates different types of arguments:

[[nodiscard]] auto f3() -> std::function<std::string(std::string)>{
    return [](cpp2::in<std::string> s) -> std::string{
        return s + " World!"; 
    }; 
}

I have also tried the code that was presented on the wiki (https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators#-and-)

f: (i:int) -> * (:int) -> string = {
    return :(j:int) -> std::string = {
        return std::to_string(j);
    };
}

but it also failed:

bug_std_function.cpp2(1,17): error: '*'/'const' type qualifiers must be followed by a type name or '_' wildcard (at '(')
bug_std_function.cpp2(1,19): error: missing function return after -> (at 'int')

I would like to be able use cpp2 function types as templates arguments, or function argument and return types.

From what I understand from the wiki my code should be written in the following way:

f: () -> std::function<(:std::string) -> std::string> = {
    return :(s : std::string) -> std::string = {
        return s + " World!";
    };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions