Why Strings as Classes?
Benji Smith
dlanguage at benjismith.net
Mon Aug 25 17:10:18 PDT 2008
Sorry, dan. You're wrong.
superdan wrote:
> again you want to build larger components from smaller components.
Good APIs define public interfaces and hide implementation details,
usually providing a default general-purpose implementation while
allowing third-parties to define special-purpose implementations to suit
their needs.
In D, the text handling is defined by the data format (unicode byte
sequences) rather than by the interface, while providing no polymorphic
mechanism for alternate implementations.
It's the opposite of good API design.
The regular expression engine accepts character arrays. And that's it.
You want to match on a regex pattern, character-by-character, from an
input stream? Tough nuts. It's not possible.
The new JSON parser in the Tango library operates on templated string
arrays. If I want to read from a file or a socket, I have to first slurp
the whole thing into a character array, even though the
character-streaming would be more practical.
Parsers, formatters, console IO, socket IO... Anything that provides an
iterable sequence of characters ought to comply with an interface
facilitating polymorphic text processing. In some cases, there might be
a slight memory/speed tradeoff. But in many more cases, the benefit of
iterating over the transient characters in a stream would be much faster
and require a tiny fraction of the memory of the character array.
There are performance benefits to be found on both sides of the coin.
Anyhow, I totally agree with you when you say that "larger components"
should be built from "smaller components".
But the "small components" are the *interfaces*, not the implementation
details.
--benji
More information about the Digitalmars-d
mailing list