-
Notifications
You must be signed in to change notification settings - Fork 260
[QUESTION] how to express this code in CppFront (is it possible)? #1235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Default arguments are currently not supported in cppfront. But Herb plans on doing so. Until then one would currently write:
I currently can not say anything about the structured binding. But this is probably also on the todo list. |
This was already done as part of: 873b760, so I think this is just a bug, although the C++ version is taking |
@MaxSagebaum another question and struggle I keep having about syntax (that you used): Is the syntax below valid cpp2?
For what I read in the docs I would assume that no. To create a temporary (that is a function call in theory I assume and initialization and function calls seem to have disjoint syntax in cpp2), you should do the following:
Is my assumption correct? |
Ah ok, my bet.
Yes you are just creating an object via the constructor call and then call the function Does this answer your question? |
In C++ yes, what I am not sure is if that is valid Cppfront syntax for temporary construction, as far as I understand, and assuming the grammar is context-free, std::plus<>() would be ambiguous from a context-free grammar point of view. So correct me if I am wrong, but this is what I believe to know so far:
I do not see anywhere (and had compile errors I recall) where you can use function syntax to initialize a variable via a constructor. But I might be wrong and have missed something. I read all the docs for CppFront twice. |
You might be right that this is per se not context free. But it is currently working. The current disambiguation rules I do not know by heart but I think cppfront uses the following:
@hsutter: Is this correct. Maybe we should add it to the documentation. |
@MaxSagebaum not sure if you are following what I meant, actually it was not about the template parameters ambiguity. What is ambiguous is this syntax (in case it is allowed in CppFront the first thing):
In order to know that the first is a constructor call and not a function call, I need to know that
That is unambiguously initializing an object. As it can be seen it does not overlap the call syntax (it has an equals sign in-between the parameter list and the type) and you do not need to know if your symbol is a type or a function, so you do not need to disambiguate. You know by the syntax only it initializes a type. I wanted to confirm if your But I do not really care if it is context-free or not, just if it is legal creating an object without = in yhe middle as if it looked like a function call. |
The reason that |
Ok, this was my thesis also. So the correct syntax is the unambiguous. |
There should now be enough test cases to exercise all the places you can put default arguments Also, only emit Cpp1 lambdas as 'mutable' if there are captures (when it matters) so that pure function expressions are not 'mutable' Closes #1235
Thanks! I see there were still some bugs here, and I have a PR ready that will fix those. Re questions 1 and 3: With the above-linked PR, you can write this: combine_maps:
< AssocContainer, Func: type = std::plus<> >
( inout map1: AssocContainer, map2: AssocContainer, func: Func = () )
= {
for map2 do(kv) {
map1[kv.first] = func(map1[kv.first], kv.second);
}
}
main: () = {
m1: std::map<int, int> = ();
m1[1] = 11;
m2: std::map<int, int> = ();
m2[1] = 22;
combine_maps( m1, m2, :(x,y) x+y+33 );
std::cout << "(m1.size())$, (m2.size())$, (m1[1])$\n"; // prints: 1, 1, 66
} I just haven't committed PR #1262 directly yet because I want to keep these changes out of the way of re-rebasing #1250. Once #1250 is merged, I'll merge #1262 too and your example will work out of the box. Thanks for pointing this out! Re question 2: No, I haven't implemented decomposition / structured-bindings yet. It's on the list! Re the question about expression-scope function and object syntax: You can always write expression-scope functions and objects using the same syntax as always, but omit the name. For example, if The syntax |
The above code now works in |
Great! That is exactly what I wanted signature-wise. Thanks for your hard work.
You are welcome.
Please confirm this so that I write proper Cpp2 then... my understanding is that |
Correct, the only difference is This Cpp2 code: f( std::plus<>() );
f( :std::plus<> = () ); Lowers to this Cpp1 code: f(std::plus<>()); // note: ( )
f(std::plus<>{}); // note: { }
|
Hello community,
I am trying to translate this piece of C++ code into Cppfront:
And I tried this:
My questions are simple:
std::plus<>
as in plain C++?func: Func = ()
parameter? It seems to emit code where there isconst Func & func = <empty here>
Thank you for your time and support to everyone.
The text was updated successfully, but these errors were encountered: