new std.variant (was Re: The Right Approach to Exceptions)

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Wed Feb 22 15:51:38 PST 2012


On Wednesday, 22 February 2012 at 20:59:15 UTC, Jonathan M Davis 
wrote:
> speed [...] is really its whole point of existance. I don't 
> know why else you'd ever use appender.
[...]
> - Jonathan M Davis

A use case is to give identity to a built-in array.

Consider this:

      class MyClass {
          private MyData[] theData;

          public @property MyData[] data() {
              return theData;
          }
          ...
      }


      MyClass m = new MyClass();
      m.data ~= new MyData();
      //Nothing got appended:
      assert(m.data.length == 0);

For the 95% of the use cases, that is the desired
behaviour. You don't want anyone appending to
your private array. If you wanted to, you would
have defined MyClass.append(myData).

   But there are a few cases where you want to
give identity to the array, and let anyone who
has a "handle" to it, to be able to append it.
(another case is while porting code from languages
that don't represent arrays as ranges, and return
them as getters).

--jm






More information about the Digitalmars-d mailing list