Thread overview
Re: Voldemort command structures
Apr 22, 2012
Brad Anderson
Apr 24, 2012
CTFE-4-the-win
April 22, 2012
On Sun, Apr 22, 2012 at 4:24 AM, Gor Gyolchanyan < gor.f.gyolchanyan@gmail.com> wrote:

> Voldemort types (coined by Andrei Alexandrescu) are types, which can't
> be named. Like so:
>
> auto getMisteriousData(int i)
> {
>    struct Mysterious
>    {
>        int i;
>    }
>
>    return Mysterious(i);
> }
>
> The only way to use mysterious structure is to either do this:
>
> auto m = getMysteriousData(12);
>
> or
>
> ReturnTypeOf!getMysteriousData m;
>
> Here's a very handsome use case of this setup:
>
> class Set(T)
> {
> public:
>    auto opIndex(T datum_)
>    {
>        struct Command
>        {
>        public:
>            alias _datum this;
>
>            bool contains() @property
>            {
>                return (_datum in _set) !is null;
>            }
>
>            void add()
>            {
>                _set._data[_datum] = false;
>            }
>
>            void remove()
>            {
>                _set._data.remove(_datum);
>            }
>
>        private:
>            Set _set;
>            T _datum;
>        }
>    }
>
> private:
>   bool[T] _data;
> }
>
> this will allow you do use Set class like this:
>
> auto s = new Set!int;
> assert(!s[6].contains);
> s[6].add();
> assert(s[6].contains);
> s[6].remove();
> assert(!s[6].contains);
>
> This will get even better if the voldemort Command structure, returned by opIndex would include the same pattern, allowing to chain the parameters and actions in any way desirable.
>
> --
> Bye,
> Gor Gyolchanyan.
>

That's really neat.  We need to start making a catalog of interesting stuff you can do in D.

Regards,
Brad Anderson


April 23, 2012
On 4/22/12 11:42 AM, Brad Anderson wrote:
> That's really neat.  We need to start making a catalog of interesting
> stuff you can do in D.

We need descriptions in article or blog entry form, and then we collect (links to) them on the website.

Andrei

April 24, 2012
On Sunday, 22 April 2012 at 18:42:19 UTC, Brad Anderson wrote:
> On Sun, Apr 22, 2012 at 4:24 AM, Gor Gyolchanyan <
> gor.f.gyolchanyan@gmail.com> wrote:
>
>> Voldemort types (coined by Andrei Alexandrescu) are types, which can't
>> be named. Like so:
>>
>> auto getMisteriousData(int i)
>> {
>>    struct Mysterious
>>    {
>>        int i;
>>    }
>>
>>    return Mysterious(i);
>> }
>>
>> The only way to use mysterious structure is to either do this:
>>
>> auto m = getMysteriousData(12);
>>
>> or
>>
>> ReturnTypeOf!getMysteriousData m;
>>
>> Here's a very handsome use case of this setup:
>>
>> class Set(T)
>> {
>> public:
>>    auto opIndex(T datum_)
>>    {
>>        struct Command
>>        {
>>        public:
>>            alias _datum this;
>>
>>            bool contains() @property
>>            {
>>                return (_datum in _set) !is null;
>>            }
>>
>>            void add()
>>            {
>>                _set._data[_datum] = false;
>>            }
>>
>>            void remove()
>>            {
>>                _set._data.remove(_datum);
>>            }
>>
>>        private:
>>            Set _set;
>>            T _datum;
>>        }
>>    }
>>
>> private:
>>   bool[T] _data;
>> }
>>
>> this will allow you do use Set class like this:
>>
>> auto s = new Set!int;
>> assert(!s[6].contains);
>> s[6].add();
>> assert(s[6].contains);
>> s[6].remove();
>> assert(!s[6].contains);
>>
>> This will get even better if the voldemort Command structure, returned
>> by opIndex would include the same pattern, allowing to chain the
>> parameters and actions in any way desirable.
>>
>> --
>> Bye,
>> Gor Gyolchanyan.
>>
>
> That's really neat.  We need to start making a catalog of interesting stuff
> you can do in D.
>
> Regards,
> Brad Anderson

Be sure to add support for this also in the example:

if(auto e = s[6]) // implicit contains
  e.remove();