Why explicit cast?

Bill Baxter dnewsgroup at billbaxter.com
Mon Feb 12 12:42:04 PST 2007


Andreas Kochenburger wrote:
> Jarrett Billingsley wrote:
>> "Andreas Kochenburger" <akk at nospam.org> wrote in message 
>> news:eqo451$8p9$1 at digitalmars.com...
>>
>>> The C-syntax is as clear as tap water, so why had this complication 
>>> been added?
>>
>> After having used D for more than two years, I'll have to disagree 
>> with this comment ;)  Besides, as Joel said, your code really 
>> shouldn't have a lot of casts in it, unless you're programming "in C" 
>> in D. 
> 
> First thanks to all those who have answered.
> 
> Indeed I am programming "in C" in D i.e. I am converting a small 
> compiler from C to D, to test if D would make life easier for me.
> The program uses a lot of pointers of different types to address bits 
> and bytes and ints and floats etc. in a memory region directly. Probably 
> this kind of system programming is not an ideal application for D.

Why not?  You can certainly make pointers and manipulate pointers all 
you want with D.

> I got also a bit frustrated about the (at least for me, messy) "very" 
> flexible string handling in D: static arrays, dynamic arrays, strings, 
> C-like strings (to be converted with toStringz), then I have to import 
> std.c.string and std.string and then I have to add .ptr to a pointer to 
> a static array string, when I use the C-function strtok from 
> std.c.string it claims char* pointers and does not want char[] because 
> it is a static array and its name is not the pointer to its first 
> element etc etc ..... grrrrrgh

Seems like your problem is trying to use too much stuff from std.c.  All 
that stuff is there as a last resort, really.  If you use D libraries to 
do the job you won't need toStringz and .ptr everywhere.  Even if you do 
want to use a lot of std.c stuff, you can write a more D-like wrapper 
around it to hide the toStringz so you don't go crazy.

--bb



More information about the Digitalmars-d-announce mailing list