short thoughts on D (like my twitter)

Adam D. Ruppe destructionator at gmail.com
Fri Jun 10 15:19:37 PDT 2011


Nick Sabalausky wrote:
> But I do have something that just happens to arguably be a lot like
> a blog and uses a blogging engine ;)

Gah, only weens use blogging engines!

I tend to just write my stuff as plain html files (like you can see
here). Sometimes I'll factor out common things, but I usually
don't venture far from plain text.


On the feed issue, that's something that doesn't bug me either -
I just keep a list of sites I like in my brain and check them
whenever I have nothing better to do. This perhaps only works
for me because I read so few sites!


> It'd be better if there were just some sort of variant of "to"
> that indicated failure some other way (and "to" could be built
> out of it.)

Indeed, but nothing in phobos currently can quite do that...


Some quick commentary on IFTI:

I actually discovered this by accident. Of course, I use IFTI
all over the place, like most D programmers probably do.

But, since the T argument was a default one here, I often didn't
specify it:

int a = cgi.request!int("a");

(Why use this instead of to!int(cgi.get["a"])? The request
 implementation checks both get and post.)

Then, I started adding it, but still specified:

int a = cgi.request!int("a", 100);


One time, I just didn't write the template argument and it
still worked!


While it's a really mundane feature of D, I still felt a bit
of "hey cool" when it worked.



The to!enum was another thing I didn't expect. I thought it would
do the same as casting an int, but it works from name, which is
actually very cool. More user friendly, and that white listing
aspect is also pretty useful.

mysql.query("select * from users where " ~
  to!string(cgi.request("field", Field.name)) ~
" = ?", value);


Building a sql string like that is fairly ugly, and normally, it'd
be /completely/ insane. You're just begging for trivially easy
sql injections.


But, thanks to the enum acting as a whitelist, you actually can
do that in D.


(Note that while I'm putting this in the web.d directory and talking
about cgi, lots of this stuff works on the command line too. Imagine
an enum for command line flags - converting is easy, you can
to!string one of the enums safely, you can list the arguments
using reflection, and final switch() can be used to ensure you
cover them all!

D's enums have a lot of hidden treasures.)


More information about the Digitalmars-d-announce mailing list