msoucy / dproto

D Protocol Buffer mixins to create structures at compile time
Boost Software License 1.0
37 stars 16 forks source link

how to set optional message fields? [got: Called `get' on null Nullable!Foo] #99

Closed timotheecour closed 7 years ago

timotheecour commented 7 years ago

message MymsgAux{ optional string foo1=1; }

message Mymsg{ required MymsgAux a1=1; optional MymsgAux a2=2; }

Mymsg proto; proto.a1.foo1="bar"; // ok proto.a2.foo1="bar"; // Called `get' on null Nullable!MymsgAux

Adding proto.a2 = MyMsgAux(); gave: Error: template std.typecons.Nullable!(MyMsgAux).Nullable.opAssign cannot deduce function from argument types !()(MyMsgAux)

timotheecour commented 7 years ago

figured out what was happening:

template protoType(string Tname, string file_protodef){
  enum protodef=import(file_protodef);
  import dproto.dproto;
  mixin ProtocolBufferFromString!protodef;
  mixin("alias protoType="~Tname~";");
}

// NOTE: defs.proto contains messages

alias Mymsg=protoType!("Mymsg","defs.proto");
alias MymsgAux=protoType!("MymsgAux","defs.proto");

Mymsg proto;
proto.a2 = MyMsgAux(); // Error: template std.typecons.Nullable!(MyMsgAux).Nullable.opAssign cannot deduce function from argument types !()(MyMsgAux)

Unfortunately, the error message was very confusing since the name MyMsgAux matches.