Head Const

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 16 00:03:31 PST 2016


On Tuesday, 16 February 2016 at 07:46:11 UTC, w0rp wrote:
> I think if we could avoid putting head const in the language, 
> we'd be better off. If you ever really, really need to mutate 
> something inside a const type, you can cast away const. It will 
> rightly look ugly, and the compiler can complain that it's not 
> @safe.

Actually, in D, it's undefined behavior to cast away const and 
mutate. D's const is supposed to guarantee that the data cannot 
be mutated via a const reference to the data. Given that very few 
optimizations can be made based solely on const (and I'm not sure 
that the compiler currently does any of them), it will almost 
certainly work as long as the underlying data is mutable rather 
than immutable, but it _is_ undefined behavior, and you're 
violating the guarantees that the type system provides when you 
go around it like that.

- Jonathan M Davis


More information about the Digitalmars-d mailing list