standard ranges

Jonathan M Davis jmdavisProg at gmx.com
Wed Jun 27 22:10:28 PDT 2012


On Thursday, June 28, 2012 08:59:32 Gor Gyolchanyan wrote:
> On Thu, Jun 28, 2012 at 12:22 AM, Steven Schveighoffer
> 
> <schveiguy at yahoo.com> wrote:
> > On Wed, 27 Jun 2012 15:20:26 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:
> >> There is no reason for anyone to be confused about this endlessly. It
> >> is simple to understand. Furthermore, think about the implications of a
> >> library-defined string type: it just introduces the problem of what the
> >> type of built-in string literals should be. This would cause endless
> >> pain with type deduction, ifti, string mixins, ... A library-defined
> >> string type cannot be a full string type. Pretending that it can has no
> >> value.
> > 
> > Default type of the literal should be the library type.  If you want
> > immutable(char)[], use "abc".codeunits or equivalent.
> > 
> > Of course, it should by default work as a zero-terminated char * for C
> > compatibility.
> > 
> > The current situation is not simple to understand.  Generic code that
> > accepts arrays has to special-case narrow-width strings if you plan to use
> > phobos with them in some cases.  That is a horrible situation.
> > 
> >> alias immutable(char)[] string is just fine.
> > 
> > That is technically fine, but if phobos wants to treat immutable(char)[]
> > as
> > something other than an array, it is not fine.
> > 
> > -Steve
> 
> Currently strings below dstring are only applicable in ForwardRange
> and below, but not RandomAccessRange as they should be.

Except that they shouldn't be, because you can't do random access on a narrow 
string in O(1). If you can't index or slice a range in O(1), it has no 
business having those operations. The same goes for length. That's why narrow 
strings do not have any of those operations as far as ranges are concerned. 
Having those operations in anything worse than O(1) violates the algorithmic 
complexity guarantees that ranges are supposed to provide, which would 
seriously harm the efficiency of algorithms which rely on them. It's the same 
reason why std.container defines the algorithmic complexity of all the 
operations in std.container. If you want a random-access range which is a 
string type, you need dchar[], const(dchar)[], or dstring. That is very much 
on purpose and would not change even if strings were structs.

- Jonathan M Davis


More information about the Digitalmars-d mailing list