cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Namespaces

Open filipsajdak opened this issue 2 years ago • 7 comments

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.

filipsajdak avatar Oct 08 '22 08:10 filipsajdak

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.

switch-blade-stuff avatar Oct 08 '22 12:10 switch-blade-stuff

image

Well this is what Herb proposed.

filipsajdak avatar Oct 08 '22 13:10 filipsajdak

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.

switch-blade-stuff avatar Oct 08 '22 14:10 switch-blade-stuff

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 = {/**/}
}

filipsajdak avatar Oct 09 '22 09:10 filipsajdak

Given modules, are namespaces still needed?

ntrel avatar Oct 09 '22 14:10 ntrel

Yes, they're orthogonal.

JohelEGP avatar Oct 09 '22 14:10 JohelEGP

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.

jcanizales avatar Oct 10 '22 16:10 jcanizales

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!

hsutter avatar Nov 26 '22 17:11 hsutter

I love namespace_class approach.

filipsajdak avatar Nov 26 '22 21:11 filipsajdak