-
Notifications
You must be signed in to change notification settings - Fork 57
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
atdcpp: recursive types render cpp code that fails to compile #414
Comments
I misspoke. This is not a problem with forward declarations. std::variant doesn't work well for defining recursive types. |
@elrandar FYI |
Some experimentation and borrowing from the last entry here. Given mutually recursive types:
then it seems it works to abstract the recursive type references as parameters, and use structs to tie the knot:
A more experienced C++ dev may have a more elegant solution. |
My code above is bogus. It would seem that one can define recursive algebraic types with |
Hi, thank you for opening that issue. It is a known problem and I spend some time trying to find the right way to tackle it when implementing the C++ backend. The solution I've landed on, which is quite heavy, but has the benefit of being easy to understand is; as you described above; to wrap the recursive references in For that matter I introduced the It can be done as so, using the helper functions already included : type recursive_record2 = {
id: int;
flag: bool;
children: recursive_record2 nullable wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">;} The output will be struct RecursiveRecord2 {
int id;
bool flag;
std::shared_ptr<std::optional<typedefs::RecursiveRecord2>> children;
static RecursiveRecord2 from_json(const rapidjson::Value & doc);
static RecursiveRecord2 from_json_string(const std::string &s);
static void to_json(const RecursiveRecord2 &t, rapidjson::Writer<rapidjson::StringBuffer> &writer);
static std::string to_json_string(const RecursiveRecord2 &t);
std::string to_json_string();
}; You can find more examples in the tests. Similarly for a variant: type recursive_variant = [
| Integer of int
| Rec of recursive_variant wrap <cpp templatize t="std::shared_ptr" wrap="_atd_wrap_shared_ptr" unwrap="_atd_unwrap_shared_ptr">
] I would direct you towards looking at the Cpp test for these kind of cases, although the backend obviously needs more documentation. |
First of all .. thanks to everyone who contributes to these tools.
Unfortunately I have run into a problem with atdcpp when using recursive types. Given the following (
recurisive.atd
):I can run
atdcpp
to producerecursive_atd.cpp
andrecursive_atd.hpp
. Compiling with:yields a ton of error messages including:
which to me, as a newbie C++ developer, suggests trouble with the forward declarations.
The text was updated successfully, but these errors were encountered: