cppfront
cppfront copied to clipboard
[SUGGESTION] Namespaces
I want to use namespaces in the cpp2. Do you have any plan for syntax that will be used for it?
I know that the code is identified as cpp2 by
// Switch to cpp2 mode if we're not in a comment, not inside nested { },
// and the line starts with "nonwhitespace :" but not "::"
Based on my current experience with cppfront I try:
N : namespace = {
fun: (i : _) -> auto = { return i * 10; }
}
it produces
namespace N { {
[[nodiscard]] auto fun(auto const& i) -> auto{return i * 10; }
} };
close enough - too many braces. (https://godbolt.org/z/f61h9W8Mn)
To my surprise, this worked
N : namespace = fun: (i : _) -> auto = { return i * 10; }
and produces
namespace N { [[nodiscard]] auto fun(auto const& i) -> auto{return i * 10; } };
(https://godbolt.org/z/nWWbs7d3K)
In lower_to_cpp1()
it accidentally matches
// Object with optional initializer
else if (!printer.doing_declarations_only() && n.is(declaration_node::object))
the fun part is that using the same pattern we can define a struct
or a class
N : struct = i:int = 42;
that produces
struct N { int i { 42 }; };
Voilà! cppfront has class support ;) (https://godbolt.org/z/89aPas1nr)
Probably the syntax should be
N : namespace = {
fun: (i : _) -> auto = { return i * 10; }
}
What do you think?
I am trying to introduce the change in the cppfront that enables it but I need to spend some time to understand how it works to be able to spot the right place.
PS: I didn't know what type of issue to select - sorry for making a mess.
Personally, i am definitely not a fan of the postfix class/struct/namespace keywords, those arent types after all, and would probably make parsing harder. Also how would you define inheritance if the postfix is taken up by the keyword.
Well this is what Herb proposed.
This is true, im just thinking of how the syntax around template types, inheritance and concepts going to work, ill make a separate issue to discuss this later.
I just have a second thought after writing some code with currently available syntax
execspec: namespace = execute: (cmd : std::string) -> auto = {/**/}
execspec: namespace = create_temporary_directory: () -> auto = {/**/}
execspec: namespace = compile_cpp2: (code : std::string, headers : std::vector<std::string>) -> std::string = {/**/}
Maybe, for such cases we don't need the namespace
keyword at all. We could rewrite above example to:
execspec::execute: (cmd : std::string) -> auto = {/**/}
execspec::create_temporary_directory: () -> auto = {/**/}
execspec::compile_cpp2: (code : std::string, headers : std::vector<std::string>) -> std::string = {/**/}
I think it should be one of the accepted ways of declaring something inside the namespace. Of course, it will generate a lot of boilerplate for cases where everything should be in some nested namespace... then probably it should be handled by:
execspec: namespace = {
execute: (cmd : std::string) -> auto = {/**/}
create_temporary_directory: () -> auto = {/**/}
compile_cpp2: (code : std::string, headers : std::vector<std::string>) -> std::string = {/**/}
}
Given modules, are namespaces still needed?
Yes, they're orthogonal.
Well this is what Herb proposed.
No need to worry about backwards compatibility here though.
But one or the other might be easier for the grammar.
Thanks everyone... to answer the question, yes there'll be namespace support.
I've now added a Design note: Namespaces to answer this question.
Thanks!
I love namespace_class
approach.