diff --git a/docs/field_syntax.md b/docs/field_syntax.md index 09e17399..08e023b0 100644 --- a/docs/field_syntax.md +++ b/docs/field_syntax.md @@ -1,8 +1,8 @@ -# Structs +# The `rfl::Field`-syntax -In order to setup a struct, all of its fields need to be an `rfl::Field`. The `rfl::Field` contains the type and the name. -The name is a compile-time string and won't have any runtime overhead. The compile-time string is what will eventually -appear in the JSON (or any other format). +As we have shown, reflect-cpp can automatically detect field names from structs. + +However, it also possible to annotate the fields using `rfl::Field`, like this: ```cpp struct Person { @@ -12,6 +12,13 @@ struct Person { }; ``` +Even though this requires slightly more effort from the programmer, it has an important +advantage: These field names are known at *compile time*, whereas the field names which +are retrieved automatically are only known at *runtime*. Even though the runtime overhead +should be negligible in most cases, this allows for compile-time checks +which are otherwise impossible. It also allows for functionalities like `rfl::replace`, +`rfl::as` or interactions with `rfl::NamedTuple` which we will see later in the documentation. + You can initilize your struct like this: ```cpp @@ -71,17 +78,3 @@ And you can parse it back into a struct: const auto homer = rfl::json::read(json_string).value(); ``` -## Important note - -**Do not create custom constructors on the structs.** - -reflect-cpp needs to be able to create the structs like this: - -```cpp -Person{rfl::Field<"firstName", std::string>("Bart"), rfl::Field<"lastName", std::string>("Simpson"), ...}; -``` - -But if you create a custom constructor, then C++ will no longer allow this kind of constructions. - -If you want to create the struct from one of your classes (the most like reason, you want to create custom constructors in the first place), -you might want to check out the section on [custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or [custom parsers](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md). diff --git a/docs/named_tuple.md b/docs/named_tuple.md index 406c568d..3d192595 100644 --- a/docs/named_tuple.md +++ b/docs/named_tuple.md @@ -125,26 +125,6 @@ using Shapes = std::variant, rfl::Field<"square", Square>>; ``` -They can also be inserted into `rfl::TaggedUnion`: - -```cpp -using Circle = rfl::NamedTuple< - rfl::Field<"shape", rfl::Literal<"Circle">>, - rfl::Field<"radius", double>>; - -using Rectangle = rfl::NamedTuple< - rfl::Field<"shape", rfl::Literal<"Rectangle">>, - rfl::Field<"height", double>, - rfl::Field<"width", double>>; - -using Square rfl::NamedTuple< - rfl::Field<"shape", rfl::Literal<"Square">>, - rfl::Field<"width", double>>; - -// Tell rfl::TaggedUnion that you want it to look for the field "shape". -using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -``` - ### `rfl::replace` `rfl::replace` works for `rfl::NamedTuple` as well: @@ -214,7 +194,7 @@ const auto employee = Employee( ## Transforming structs to named tuples and vice versa -You can transform structs to named tuples and vice versa: +You can transform structs to named tuples and vice versa (this will only work with the `rfl::Field`-syntax: ```cpp auto bart = Person{.first_name = "Bart", @@ -236,15 +216,3 @@ const auto bart_struct = rfl::from_named_tuple(bart_nt); const auto bart_struct = rfl::from_named_tuple(std::move(bart_nt)); ``` -## rfl::NamedTuple is central to reflect-cpp - -`rfl::NamedTuple` is basically at the heart of how the reflect-cpp reflection -is actually implemented. If you look at how reflect-cpp is implemented you will find that -structs are transformed from and to `rfl::NamedTuple`s -all the time (using zero-copy operations wherever possible). -That is because `rfl::NamedTuple` actually support reflection, -where as structs don't. - - - -