My Issues with Slices and AAs

Kevin Cox kevincox.ca at gmail.com
Fri Mar 2 05:33:06 PST 2012


After learning and using D for a little while I have discovered some (in my
opinion) problems with the slices and associative array built-ins (for now
I will just say slice).  The main issue I have is that there is no way to
pass around something that looks like and acts like a slice.  This is
because there is no class or interface to which slices adhere.  I think
this is a very simple concept and it could be easily remedied by creating
an Array interface which slices implement.  This way you could create
things like lazy and asynchronously filled arrays.

I have to admit that I am a little at a loss for examples where a slice
could be used this way but I would like to have the power because that is
the heart of polymorphism.  I could design a class/function/... that takes
an "object that implements Array" and if later I find out that I would like
to pass a slightly different array I could do that without any changes to
my code.  An example of this is an old issue from C++.  RapidXML (you
guessed it, an XML library) was initially only able to read from a file.
This means that if you were doing something (like a webserver application)
you would have to write the incoming XML to disk before parsing it, losing
all of the speed on RapidXML.  The thing was that this design choice was
because it was a lot of work to support both efficiently and requiring
strings as input meant that most users would have to load a whole file into
memory before starting the parsing process.  If we were to write DXML we
could instead require an input that is an array of characters and we could
pass in either a string or a file (or file reader, or whatever) that read
more of the file as it was accessed.  (of course doing some nice buffering
and the like)  This is because all DXML wants is a bunch of characters, it
doesn't care where they are coming from.

An example for AAs is if you want to do something like live translation in
your app.  It makes sense to have an AA of strings for every language.
But, if every language is not complete you end up with holes.  Instead you
decide you wish to fallback to a complete language.  Now, you have to
rewrite your app so that every function that displays stuff to the User
takes both the locale and the fallback.  Or, you create an AA class that
handles lookup for you.  But you still can't pass this around because your
program is wired for AAs.

In, conclusion. (Tl:Dr) slices and AAs do not allow polymorphism and
therefore are decreasing the power and flexibility of D.

I hope to hear your opinions,
Kevin.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120302/eb3de2ed/attachment.html>


More information about the Digitalmars-d mailing list