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.
  • Undo in D DigitalDesigns via Digitalmars-d-learn
    • Re: Undo in D Basile B. via Digitalmars-d-learn
    • Re: Undo in D bauss via Digitalmars-d-learn

Reply via email to