C tips (again)
bearophile
bearophileHUGS at lycos.com
Sat May 2 09:40:10 PDT 2009
Andrei Alexandrescu:
> (untested)
> struct Mystring {
> size_t size, capacity;
> char* pdata() { return cast(char*) (&this + 1); }
> char[] data() { return pdata()[0 .. size]; }
> }
It looks quite more complex than the C code (but Denis Koroskin has found a simple solution).
>but really it's more of a proof that with ingenuity and discipline a bear can be made to dance.<
Bears can do many things that most humans can't, like telling apart two homozygote twins from scent alone :-)
>I don't see how that stuff could help D any more than a coconut and two straws could help a radar receiver.<
>From reading the document, and from some experience in programming D1, I think there are some missing things in D:
1) Something like the run-time "throws" of my dlibs, that given a list of one or more exceptions, and an expression, run-time asserts they the expression throws one of those exceptions. Othrwise prints the line where the Throws is (and not another line).
This is very useful for unittests.
At the moment I use:
assert(Throws!(exceptions list...)(exp));
that is bad looking and nonstandard.
2) A compile-time "static throws". That asserts that a compile-time expression asserts, otherwise prints the line number where the "static throws" is.
This is very useful for unittests of compile-time things, to assert they fail when they must fail.
Time ago someone here has shown me a trick to do this (using is()).
I'll ask again for such two things in the future, because they are basic, it's not easy to do .
~~~~~~~~~
More ideas from those ctips:
The following idioms may be useful as an optional built-in idiom in D programs, to improve debugging:
http://users.bestweb.net/~ctips/tip015.html
http://users.bestweb.net/~ctips/tip022.html
http://users.bestweb.net/~ctips/tip024.html
http://users.bestweb.net/~ctips/tip026.html
http://users.bestweb.net/~ctips/tip027.html
http://users.bestweb.net/~ctips/tip028.html
http://users.bestweb.net/~ctips/tip032.html
Disallowing undefined situations from D is good, like disallowing lines of code with certain undefined side effects (another possible solution is to do as Java, and make them defined):
http://users.bestweb.net/~ctips/tip037.html
Collection of allocation statistics can be quite useful:
http://users.bestweb.net/~ctips/tip044.html
Built-in leak detection and tracing, this is primitive (but better than nothing). Better ideas can be used:
http://users.bestweb.net/~ctips/tip045.html
http://users.bestweb.net/~ctips/tip019.html
http://users.bestweb.net/~ctips/tip020.html
http://users.bestweb.net/~ctips/tip062.html
>I have been mightily disappointed (essentially my respect for the author has crashed into the ground) when I saw that the entire optimization example, although clearly a lot of time and effort has been put into it, had absolutely no numbers whatsoever.<
Even if you haven't appreciated a detail, you can ignore all the other good things of those ctips.
---------------------
Denis Koroskin:
> Mystring s = ...;
> char c = s.data.ptr[42];
Thank you, I can use this one.
Bye,
bearophile
More information about the Digitalmars-d
mailing list