dproto icon indicating copy to clipboard operation
dproto copied to clipboard

getters and setters should work without calling `init` on Nullable fields, as in C++

Open timotheecour opened this issue 7 years ago • 1 comments

@msoucy

in C++ we can write:

message MyMessage{
  optional Foo foo=1;
}
message Foo{
  optional Bar bar=1;
}
message Bar{
  optional string baz=1;
}
MyMessage a;
a.mutable_foo()->mutable_bar()->set_baz("hello");

in dproto we need to call init on the intermediate fields foo, bar

auto initialize_nullable(T:Nullable!U, U)(ref T a){ a=U.init; }

MyMessage a;
a.foo.initialize_nullable;
a.foo.bar.initialize_nullable;
a.foo.bar.baz="hello";

Would be nice to not have to call init and allow this:

MyMessage a;
a.foo.bar.baz="hello";

I believe this could be implemented via a modification of Nullable that would call init on 1st access to a setter or getter.

any thoughts on this?

EDIT: asking http://forum.dlang.org/post/[email protected] for [Nullable with auto-allocation on access]

timotheecour avatar Jun 14 '17 07:06 timotheecour

The idea seems pretty good.

heketang avatar Jun 15 '17 02:06 heketang