More constants in std.string?

simendsjo simen.endsjo at pandavre.com
Mon Aug 9 18:00:44 PDT 2010


On 10.08.2010 02:46, Jonathan M Davis wrote:
> On Monday, August 09, 2010 17:24:47 simendsjo wrote:
>> \r and \n is used many times throughout std.string (and probably other
>> modules like file).
>> I assume defining constants were considered:
>> const CR = '\r';
>> const LF = '\n';
>>
>> So why isn't constants used instead?
>
> Well, what would that really save or help? They're totally unambiguous and
> easily typed with the keyboard. Not to mention, if you were going to do that,
> you'd probably define them like so
>
> enum CR = "\r";
> enum LF = "\n";
>
> Using enum for manifest constants is the more typical way to do it in D (it also
> forces compile-time evaluation of their value), and we want to avoid using
> individual chars or wchars, so pretty much all string-related functions take
> strings even if you'd think that they'd take a character of some kind (though if
> they were to take a character type, it would have to be dchar).
>
> In any case, the values for these constants never change regardless of the
> machine - unlike something like path.sep which indicates the path separator for
> the machine and changes depending on which machine you compile on. For it to be
> worth making "\r" and "\n" constants, you have to gain something from it, and I
> don't think that there's much to be gained. Not to mention, you can always make
> more constants, and you have to stop somewhere, so you're always going to find
> something that _could_ be a constant and isn't.
>
> - Jonathan M Davis

I agree. I really don't thing it makes a difference or not.. '\r' is 
always that symbol no matter if it's a iPhone, embedded device or 
supercomputer. It's more that it's more error prone to write it than CR 
(ooops! compiler error!). If I wrote '\e' in a large switch and haven't 
tested all code paths.. Yes, shame on me, but I really meant \r! A 
constant gives me an at once. D is all about removing common, stupid 
programmer errors like I do, right? :)

It's just nitpicking and not a big deal at all. I can only remember once 
I've been bitten by one of these (not in D), but I quickly found the source.


More information about the Digitalmars-d-learn mailing list