Casting away const

2010-08-08 Thread simendsjo
I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" // ptr to const int const(int)* p; int* q = cast(int*)p; *q = 3; // undefined behavior But why would

Re: Casting away const

2010-08-08 Thread bearophile
simendsjo: > But why would you want to cast away const then? I presume for a situation where you are sure your code is just reading the information and not modifying it, and you need something that is not const. > assert(i == 10); // i still 10 though.. How is this possible? You have don

Re: Casting away const

2010-08-08 Thread div0
On 08/08/2010 22:56, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" Casting away const is mostly for dealing with external non D libra

Re: Casting away const

2010-08-08 Thread BCS
Hello simendsjo, I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" // ptr to const int const(int)* p; int* q = cast(int*)p; *q = 3; // undefined behavi

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" I thought it was "you're on y

Re: Casting away const

2010-08-09 Thread bearophile
Steven Schveighoffer: > I thought it was "you're on your own", not undefined behavior. The former > implies there is some "right" way to do this if you know more about the > data than the compiler, the latter implies that there is no right way to > cast away const. Am I wrong? In my opinio

Re: Casting away const

2010-08-09 Thread Don
Steven Schveighoffer wrote: On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" I thought it w

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
e data can't be undefined behaviour, otherwise casting const away is useless and can be totally disallowed. Casting away const just to read the data is useless. You can read const data without a cast. But my understanding is that casting away const to write should be doable if you know

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 10:11:39 -0400, Don wrote: Steven Schveighoffer wrote: On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const"

Re: Casting away const

2010-08-09 Thread Don
nd then reading the data can't be undefined behaviour, otherwise casting const away is useless and can be totally disallowed. Casting away const just to read the data is useless. You can read const data without a cast. No you can't. You can't pass it to a C function. But my

Re: Casting away const

2010-08-09 Thread BCS
Hello Steven, On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" I thought it was "y

Re: Casting away const

2010-08-09 Thread BCS
Hello Steven, On Mon, 09 Aug 2010 10:11:39 -0400, Don wrote: Steven Schveighoffer wrote: On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
d its const nature with a cast. Just casting const away and then reading the data can't be undefined behaviour, otherwise casting const away is useless and can be totally disallowed. Casting away const just to read the data is useless. You can read const data without a cast. No you c

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
spec says: "modification after casting away const" => "undefined behavior" I thought it was "you're on your own", not undefined behavior. The former implies there is some "right" way to do this if you know more about the data than the compiler, the l

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 10:24:56 -0400, BCS wrote: Hello Steven, On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefin

Re: Casting away const

2010-08-09 Thread BCS
reading the data can't be undefined behaviour, otherwise casting const away is useless and can be totally disallowed. Casting away const just to read the data is useless. You can read const data without a cast. But my understanding is that casting away const to write should be doable if you kn

Re: Casting away const

2010-08-09 Thread BCS
stion.. The spec says: "modification after casting away const" => "undefined behavior" I thought it was "you're on your own", not undefined behavior. The former implies there is some "right" way to do this if you know more about the data than the co

Re: Casting away const

2010-08-09 Thread BCS
Hello Steven, On Mon, 09 Aug 2010 10:24:56 -0400, BCS wrote: Hello Steven, On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo wrote: I'm totally new to the const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const"

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
const/immutable thing, so this might be a naive question.. The spec says: "modification after casting away const" => "undefined behavior" I thought it was "you're on your own", not undefined behavior. The former implies there is some "right" wa

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 11:04:40 -0400, Steven Schveighoffer wrote: On Mon, 09 Aug 2010 10:53:48 -0400, BCS wrote: OTOH that is effectively a hidden cast and has 100% of the same issues (re undefined behavior) as casting away const while being slightly harder to find. But you just said

Re: Casting away const

2010-08-09 Thread bearophile
Steven Schveighoffer: > extern(C) int strlen(const(char)* str); C has no const(char)* so I am now thinking about a possible D2 diagnostic enhancement request that turns that line of code into a compile time error :-) Bye, bearophile

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 11:15:38 -0400, bearophile wrote: Steven Schveighoffer: extern(C) int strlen(const(char)* str); C has no const(char)* so I am now thinking about a possible D2 diagnostic enhancement request that turns that line of code into a compile time error :-) But an extern(C

Re: Casting away const

2010-08-09 Thread bearophile
Steven Schveighoffer: > But an extern(C) function does not have to be written in C :) You are right. But that function written in an arbitrary language has to follow the C interface rules and limitations, and among those there is no way to define a variable to be const(char)*. So in that line o

Re: Casting away const

2010-08-09 Thread BCS
const. OTOH that is effectively a hidden cast and has 100% of the same issues (re undefined behavior) as casting away const while being slightly harder to find. But you just said that casting and reading is not undefined? Isn't this the same thing? Casting away const or tacking co

Re: Casting away const

2010-08-09 Thread Simen kjaeraas
Steven Schveighoffer wrote: class C { private Mutable!(int) cache = -1; int expensiveFunction() const { return cache == -1 ? cache = _expensiveFunctionImpl() : cache; } private int _expensiveFunctionImpl() const {...} } If this is undefined, then something like this cannot be reli

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
ut the result of opAssign. Note also that the optimization you stated is not possible, even without casting away const, but would be possible on an immutable class. But the fact that the compiler cannot peek into the implementation of the opAssign means it's forced to make no assumption

Re: Casting away const

2010-08-09 Thread Mafi
Am 09.08.2010 17:31, schrieb bearophile: Steven Schveighoffer: But an extern(C) function does not have to be written in C :) You are right. But that function written in an arbitrary language has to follow the C interface rules and limitations, and among those there is no way to define a vari

Re: Casting away const

2010-08-09 Thread Lars T. Kyllingstad
after you >>>> have removed its const nature with a cast. Just casting const away >>>> and then reading the data can't be undefined behaviour, otherwise >>>> casting const away is useless and can be totally disallowed. >>> Casting away const just to

Re: Casting away const

2010-08-09 Thread bearophile
Mafi: > I think, that isn't a good idea. I agree, that idea doesn't work well. Bye, bearophile

Re: Casting away const

2010-08-09 Thread Lutger
bearophile wrote: > Mafi: >> I think, that isn't a good idea. > > I agree, that idea doesn't work well. > > Bye, > bearophile I think it still is a good idea to forbid this in safe mode. Perhaps in trusted too.

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 14:39:58 -0400, Lutger wrote: bearophile wrote: Mafi: I think, that isn't a good idea. I agree, that idea doesn't work well. Bye, bearophile I think it still is a good idea to forbid this in safe mode. Perhaps in trusted too. Note, this isn't any less safe tha

Re: Casting away const

2010-08-09 Thread Lutger
Steven Schveighoffer wrote: > On Mon, 09 Aug 2010 14:39:58 -0400, Lutger > wrote: > >> bearophile wrote: >> >>> Mafi: I think, that isn't a good idea. >>> >>> I agree, that idea doesn't work well. >>> >>> Bye, >>> bearophile >> >> I think it still is a good idea to forbid this in safe mode.

Re: Casting away const

2010-08-09 Thread Simen kjaeraas
Steven Schveighoffer wrote: Note also that the optimization you stated is not possible, even without casting away const, but would be possible on an immutable class. But the fact that the compiler cannot peek into the implementation of the opAssign means it's forced to ma

Re: Casting away const

2010-08-09 Thread Simen kjaeraas
Simen kjaeraas wrote: Steven Schveighoffer wrote: Note also that the optimization you stated is not possible, even without casting away const, but would be possible on an immutable class. But the fact that the compiler cannot peek into the implementation of the opAssign means it&#

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 15:49:12 -0400, Simen kjaeraas wrote: Simen kjaeraas wrote: Steven Schveighoffer wrote: Note also that the optimization you stated is not possible, even without casting away const, but would be possible on an immutable class. But the fact that the compiler

Re: Casting away const

2010-08-09 Thread Jonathan M Davis
On Monday, August 09, 2010 07:13:31 Steven Schveighoffer wrote: > Casting away const just to read the data is useless. You can read const > data without a cast. That's not actually quite true. If all code were const-correct, then it would be, but much of D is not. For instance, toHa

Re: Casting away const

2010-08-09 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 17:15:41 -0400, Jonathan M Davis wrote: On Monday, August 09, 2010 07:13:31 Steven Schveighoffer wrote: Casting away const just to read the data is useless. You can read const data without a cast. That's not actually quite true. If all code were const-correct, th

Re: Casting away const

2010-08-09 Thread Jonathan M Davis
On Monday, August 09, 2010 15:01:28 Steven Schveighoffer wrote: > Then the author failed to make it const, and it's a bug in the function > definition. "Casting away const if you don't write" is crap, and should > be treated as suspiciously as code that writes to cons

Re: Casting away const

2010-08-10 Thread Steven Schveighoffer
On Mon, 09 Aug 2010 19:35:38 -0400, Jonathan M Davis wrote: On Monday, August 09, 2010 15:01:28 Steven Schveighoffer wrote: Then the author failed to make it const, and it's a bug in the function definition. "Casting away const if you don't write" is crap, and