What is nothrow for?

Yigal Chripun yigal100 at gmail.com
Fri Apr 25 13:30:23 PDT 2008


Janice Caron wrote:
> On 25/04/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
>> if the compiler provides hooks for the attribute writers than an
>>  attribute would have meaning for the compiler.
> 
> I don't agree. I think Walter's right. An attribute is a contract, and
> only the compiler is in a position to enforce that code complies with
> contracts.
> 
> For example, suppose I wanted an attribute that meant "this function
> does not modify static members" - what kind of "hooks" would make it
> possible to enforce that contract, if it were not built into the
> compiler?

if I understand you correctly, you want a function that receives an
instance of some class and an attribute that makes sure that the above
function does not modify any static members (data members) of that instance.

I'm not sure how the compiler can verify that at all due to polymorphism
- the dynamic type of the instance will be known only at run-time.

maybe something like the following can work:
you should be able to get a list of all the static members of an
instance via introspection and for each of those, you'd need to check in
your attribute implementation that the old value (before executing the
function body) is the same as the one after executing the function body.
what if there was a way to "keep" the old value? than you'd throw an
exception if (old(var) != var).

I don't understand how this can be checked by the compiler at all (even
if the attribute is not user defined) without analyzing the entire code
of the program. if you compile only one module the compiler does not
know if there are derived classes that add more static data members.

I'm not sure I'm correct on all of this so please point out errors.

somewhat confused,
Yigal



More information about the Digitalmars-d mailing list