cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] allow `: ==` for anonymous alias declarations (instead of `using`)

Open farmerpiki opened this issue 1 year ago • 7 comments

Proposed syntax:

: == <expr>;

which would compile to using <expr>;

the problem

we should allow a syntax to allow for explicitly saying just using in pure cpp2:

example 1

cpp1: using std::views::split;

in cpp2: split: == std::views::split;

compiles to... the generated code is not what I expected auto inline constexpr split{ std::views::split };

example 2

cpp1: using std::operator""sv;

cpp2: there may be a way with function aliases but it's very cumbersome to use and results in a very ugly syntax compared to the desired outcome...

PS: the purpose for the new syntax was supposed to be simple, easy to use and consistent, this just allows not repeating the name if I want to import something with just using, also it will allow importing all the overloads for a function, I don't think we have a syntax for that yet

farmerpiki avatar Aug 06 '24 11:08 farmerpiki

The using statements are already covered by Cpp2 - https://hsutter.github.io/cppfront/cpp2/namespaces/#using

vladvrabie avatar Aug 06 '24 12:08 vladvrabie

well, I was looking at alias declaration part in the documentation and it's not covered there... and with using std::operator""sv I get: error: expected ; at end of using-statement (at '""') will look into the rest, also have to check how it works with importing functions from a class

if the rest works I'll close this and write a bug report about allowing importing operators with using.

farmerpiki avatar Aug 06 '24 12:08 farmerpiki

xf: () -> int = 42; qq: namespace = { using ::xf; }

another case which doesn't work... it actually passed cppfront, but the generated code declares the using before the function declaration...

I guess I should write new bugs for this and tests for them... and make a pull request.

farmerpiki avatar Aug 06 '24 13:08 farmerpiki

@farmerpiki

with using std::operator""sv I get: error: expected ; at end of using-statement (at '""')

That's because you're missing the ; at the end as the error says. With the ; it compiles.

using std::operator""sv;

main: () = {
    std::cout << "xyzzy"sv;
}
Program returned: 0
xyzzy

However this is just passing it through as cpp1 code. If you use -p, it fails:

using.cpp2(1,1): error: pure-cpp2 switch disables Cpp1 syntax

gregmarr avatar Aug 06 '24 17:08 gregmarr

That fails as you said, but this: > cat x.cpp2

main: () = {
    using std::operator""sv;
    std::cout << "xyzzy"sv;
}

gives this: > cppfront -p x.cpp2

x.cpp2...
x.cpp2(3,24): error: expected ; at end of using-statement (at '""')

despite the obvious semicolon at the end

And I still think : == would've been nicer to use than allocating the using keyword.

farmerpiki avatar Aug 07 '24 06:08 farmerpiki

And I still think : == would've been nicer to use than allocating the using keyword.

I disagree. Or at least, I do in the current state of the syntax (because == means both using and constexpr).

I think the using statements are clear for what they do now. If the syntax changes, either for aliases or for constexpr, then I would support extending/replacing the use of using

vladvrabie avatar Aug 07 '24 08:08 vladvrabie

but it's constexpr if you have something in front of the : or am I missing something critical here?

farmerpiki avatar Aug 07 '24 09:08 farmerpiki