Undo in D

Basile B. b2.temp at gmx.com
Sat Jun 23 14:06:08 UTC 2018


On Saturday, 23 June 2018 at 01:58:31 UTC, DigitalDesigns wrote:
> Is there any idiomatic undo designs in D that give a more 
> natural implementation than the standard techniques?

- The "stuff to undo" can be a forward range ("save" primitive, + 
assignable from a stored state)
- The manager can be an output range of the "stuff to undo"


```
struct UndoManager(Undoable, Storage)
if (isForwardRange!Undoable && isOutputRange!(Storage, Undoable))
{
     Storage storage;
     Undoable undoable;
     size_t position;

     this(Storage storage, Undoable undoable)
     {}

     void push(){ storage.put(undoable.save()); length++;}

     /* etc */
}
```

So that the UndoMgr is reusable with any Undoable and Storage 
that implement the right primitives.

But well, you can do that with any PL that have basic generics, 
e.g C# or FreePascal. The key is to have a nice generic 
interface. IRL it's probably more complicated than my example. 
Storage must probably be a RandomAccessRange or have more 
specific primitives.

Now, **and this is D specific / D idiomatic**, to this can be 
applied the "design by introspection". UndoManager can allow more 
or less advanced features, depending on the primitives 
implemented by Storage and Undoable. Technically this is done at 
compile-time using __traits(hasMember), typically.


More information about the Digitalmars-d-learn mailing list