On Tue, 06 Sep 2011 13:24:34 -0400, Marco Leise <[email protected]> wrote:

Am 06.09.2011, 17:39 Uhr, schrieb Steven Schveighoffer <[email protected]>:

On Tue, 06 Sep 2011 11:11:27 -0400, Andrei Alexandrescu
Honest, C's openmode strings have been around for so long, they hardly confuse anyone anymore. I'd rather use "rw" and call it a day.

That's not a valid fopen string ;)

Sorry, but I had to laugh. There could not have been a better counter example for using fopen strings. I can live with them, but it is one of the bad designs in C that could use an alternative.

I agree, but: 1. strings are statically checkable in D (see my openFile for an example), and 2. just because the flags were poorly chosen in C doesn't mean we must adhere to that spec. In other words, "rw" is not a valid fopen mode string, but it *could* be a valid std.stdio mode string.

Enums are used in: Unix, Windows, Delphi, Haskell, Lisp, C++, C#, OCaml, Go
fopen strings are used in: C, Ruby, PHP, Python, NodeJS
Java has reinvented mode strings: http://download.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html#RandomAccessFile(java.io.File, java.lang.String) Other languages also distinguish only between a fixed set of cases, like read, write and append. I found Scala and Perl to do that.

In the end a string just like an enum with the enum being statically checked and the string being shorter. Every character corresponds to an 'ored' enum value. They can both be extended with flags that work with Windows and Posix, like 'create only if non-existent' or hints that may work on one system only, like exclusive access.

I like enums in terms of writing code that processes them, but in terms of calling functions with them, I mean look at a sample fstream constructor in C++:

fstream ifs("filename.txt", ios_base::in | ios_base::out);

vs.

File("filename.txt", "r+"); // or "rw"

There's just no way you can think "rw" is less descriptive or understandable than ios_base::in | ios_base::out.

-Steve

Reply via email to