Thread overview
implicit casts with dmd ver 1.0
Jan 19, 2007
Peter Blicharski
Jan 19, 2007
Dave
Jan 19, 2007
Johan Granberg
Jan 19, 2007
Frits van Bommel
January 19, 2007
Hello.

I'm having trouble with the sample code shipped with dmd Ver 1.0
During compilation i encounter implicit cast errors (file dhry.d)
By the way, the the dfl window library code is full of these error, too.
Isn't the dmd compiler supposed to make a cast from char[] to char*
without error? Or is it needed to always make an explicit cast?

Best regards

Peter Blicharski

January 19, 2007
Peter Blicharski wrote:
> Hello.
> 
> I'm having trouble with the sample code shipped with dmd Ver 1.0
> During compilation i encounter implicit cast errors (file dhry.d)
> By the way, the the dfl window library code is full of these error, too.
> Isn't the dmd compiler supposed to make a cast from char[] to char*
> without error? Or is it needed to always make an explicit cast?
> 

This was due to a change right before v1.0.

You can use arr.ptr to avoid the explicit cast. If it has to be null terminated (for example to pass into an extern (C) function) you can use std.string.toStringz().

> Best regards
> 
> Peter Blicharski
> 
January 19, 2007
Peter Blicharski wrote:

> Hello.
> 
> I'm having trouble with the sample code shipped with dmd Ver 1.0
> During compilation i encounter implicit cast errors (file dhry.d)
> By the way, the the dfl window library code is full of these error, too.
> Isn't the dmd compiler supposed to make a cast from char[] to char*
> without error? Or is it needed to always make an explicit cast?
> 
> Best regards
> 
> Peter Blicharski

The implicit cast from char[] to char* was deprecated shortly before 1.0.

Use this instead but be carefull as there is no guarantee that a is
nullterminated (if it is a slice it wont).
char[] a="asdsdfs";
char* b=a.ptr;
January 19, 2007
Peter Blicharski wrote:
> I'm having trouble with the sample code shipped with dmd Ver 1.0
> During compilation i encounter implicit cast errors (file dhry.d)
> By the way, the the dfl window library code is full of these error, too.
> Isn't the dmd compiler supposed to make a cast from char[] to char*
> without error? Or is it needed to always make an explicit cast?

It was removed in version v0.177 at the request of an overwhelming majority of people responding to a proposal to do so (IIRC). It's just too bug-prone, especially when interacting with C APIs. (In general, D strings aren't null-terminated)

It looks like the sample wasn't updated.

If you're sure you want this conversion to occur, you can use arr.ptr to get the pointer. If you want to ensure null-termination you are encouraged to use std.string.toStringz though.