More constants in std.string?

Jonathan M Davis jmdavisprog at gmail.com
Mon Aug 9 17:46:47 PDT 2010


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


More information about the Digitalmars-d-learn mailing list