auto decoding rant

Jonathan Shamir via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 15 08:47:54 PDT 2017


I see this is a recurring rant (I apologize if this is a 
repeating topic, I'm new to the forums). Here's an example of 
something that should be simple in D but isn't:

enum string PATTERN = "abcd";
immutable char[10] repeatingPattern = 
PATTERN.cycle().takeExactly(10).array();

The fact that front(string) returns a dchar makes some really 
simple (and common) use-cases practically impossible.

Also note I can't cast to char[] in compile time? What's the 
reason for that?

I would recommend adding something like this to phobos (and in 
all fairness, it should have been the other way around):


auto noDecode(T)(T[] str) if (isNarrowString!(T[])) {
     static struct NoDecode {
         private T[] str;

         @property ref T front() {
             assert (!this.empty);
             return str[0];
         }
         @property bool empty() {
             return (str.empty);
         }
         void popFront() {
             if (!this.empty) {
                 str = str[1 .. $];
             }
         }
         NoDecode save() {
             return this;
         }
     }
     return NoDecode(str);
}


More information about the Digitalmars-d mailing list