cast(!const) proposal

Bruno Medeiros brunodomedeiros+spam at com.gmail
Mon Sep 10 04:48:04 PDT 2007


Janice Caron wrote:
> When I mentioned this on another thread, I got misunderstood, so I'm
> going to try again, only this time being very, very clear, so
> hopefully it will all make sense this time.
> 
> I propose that it shall be a compile-time error to cast away constness
> or invariance with the existing syntax.
> 
> For example
> 
> const int n = 42;
> auto p = &n;
> auto q = cast(int *)p; /* Currently compiles. I propose that it should
> be a compile error */
> 
> Since D is a nuts-and-bolts language, and Walter doesn't actually want
> to prevent you from doing stupid things, there must be a way of
> getting round that restriction. The intent is not to /prevent/ you
> from being stupid - I mean, doing low level nuts-and-bolts things -
> it's to require you to use a different syntax, so that you damn well
> can't do it by accident. The syntax that I suggest for this is:
> 
> auto q = cast(!const)p;
> 
> This would allow the existing cast syntax to be made const-safe, and
> is also consistent with the exisiting cast(const) syntax.

This issue has been brought up before, and many of us also agree it is a 
problem:
http://www.digitalmars.com/d/archives/digitalmars/D/D2.0_an_example_of_use-case_for_casting_invariant_away_54620.html

I noted on that thread that "cast(!const)" can be made just as well 
using D's meta programming capabilites. A new syntax is not needed, 
although nonetheless, this construct should be on the standard library.


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list