On 20.11.2013 19:30, Dmitry Olshansky wrote:
20-Nov-2013 22:01, Simen Kjærås пишет:
On 20.11.2013 18:45, Simen Kjærås wrote:
[snip]
May I suggest:

struct Validated(alias fn, T) {
     private T value;
     @property inout
     T get() {
         return value;
     }

Uh-hm. Add this:
        alias get this;


And it decays to the naked type in a blink of an eye. And some function
down the road will do the validation again...

And guess what? That's (often) ok. It's better to do the validation once too many than missing it once.

The point (at least in the cases I've used it) is to enforce that only validated values are passed to functions that require validated strings, not that validated values never be passed to functions that don't really care. Doing it like this also lets you call functions that take the unadorned type, because that might be just as important.

The result of re-validating is performance loss. The result of missed validation is a bug. Also, in just a few lines, you can make a version that will *not* decay to the original type:

  struct Validated(alias fn, T) {
      private T _value;
      @property inout
      T value() {
          return _value;
      }
  }

  // validated() is identical to before.

Sure, using it is a bit more verbose than using the unadorned type, which is why I chose to make the original version automatically decay. This is a judgment where sensible people may disagree, even with themselves on a case-by-case basis.

--
  Simen

Reply via email to