www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nullable with auto-allocation on access

Is there a construct similar to Nullable that would auto-allocate upon
access (set/get) if isNull is true?

Use case: https://github.com/msoucy/dproto/issues/117 [getters and
setters should work without calling `init` on Nullable fields, as in
C++ #117]

copied inline for easy reference:

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;

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;

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

MyMessage a;

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