Skip to content
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

atdd: recursive variants and records #397

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Next Next commit
atdd: use alias this for variant with value, and improve construction
  • Loading branch information
elrandar committed Dec 1, 2023
commit f4d9982e0f39f1450e99135375b2f80103b69b5a
3 changes: 2 additions & 1 deletion atdd/src/lib/Codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,8 @@ let case_class env type_name
Line (sprintf {|// Original type: %s = [ ... | %s of ... | ... ]|}
type_name
orig_name);
Line (sprintf "struct %s { %s value; }" (trans env unique_name) (type_name_of_expr env e)); (* TODO : very dubious*)
Line (sprintf "struct %s {\n%s value; alias value this;" (trans env unique_name) (type_name_of_expr env e) );
Line (sprintf "@safe this(T)(T init) {value = init;} @safe this(%s init) {value = init.value;}}" (trans env unique_name));
Line (sprintf "@trusted JSONValue toJson(T : %s)(T e) {" (trans env unique_name));
Block [Line(sprintf "return JSONValue([JSONValue(\"%s\"), %s(e.value)]);" (single_esc json_name) (json_writer env e))];
Line("}");
Expand Down
12 changes: 9 additions & 3 deletions atdd/test/dlang-expected/everything_atd.d
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ struct Root_ {}


// Original type: kind = [ ... | Thing of ... | ... ]
struct Thing { int value; }
struct Thing {
int value; alias value this;
@safe this(T)(T init) {value = init;} @safe this(Thing init) {value = init.value;}}
@trusted JSONValue toJson(T : Thing)(T e) {
return JSONValue([JSONValue("Thing"), _atd_write_int(e.value)]);
}
Expand All @@ -387,7 +389,9 @@ struct WOW {}


// Original type: kind = [ ... | Amaze of ... | ... ]
struct Amaze { string[] value; }
struct Amaze {
string[] value; alias value this;
@safe this(T)(T init) {value = init;} @safe this(Amaze init) {value = init.value;}}
@trusted JSONValue toJson(T : Amaze)(T e) {
return JSONValue([JSONValue("!!!"), _atd_write_list!(_atd_write_string)(e.value)]);
}
Expand Down Expand Up @@ -680,7 +684,9 @@ struct A {}


// Original type: frozen = [ ... | B of ... | ... ]
struct B { int value; }
struct B {
int value; alias value this;
@safe this(T)(T init) {value = init;} @safe this(B init) {value = init.value;}}
@trusted JSONValue toJson(T : B)(T e) {
return JSONValue([JSONValue("B"), _atd_write_int(e.value)]);
}
Expand Down
2 changes: 1 addition & 1 deletion atdd/test/dlang-tests/test_atdd.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void setupTests()
obj.assoc4 = ["g": 7, "h": 8];
obj.kind = Root_().to!Kind;
obj.kinds = [
WOW().to!Kind, Thing(99).to!Kind, Amaze(["a", "b"]).to!Kind, Root_().to!Kind
WOW().to!Kind, 99.to!Thing.to!Kind, ["a", "b"].to!Amaze.to!Kind, Root_().to!Kind
];
obj.nullables = [
12.Nullable!int, Nullable!int.init, Nullable!int.init,
Expand Down