Allow const/immutable for normal functions, inner functions and delegates?

Timon Gehr timon.gehr at gmx.ch
Wed Feb 19 14:35:38 PST 2014


On 02/19/2014 11:20 PM, John Colvin wrote:
> Good idea/bad idea/already done?
>
> int ga = 1;
> const int gb = 2;
> immutable int gc = 3;
>
> int foo() immutable
> {
>      // reference to gc is legal
>      // any reference to ga or ba is an error
> }
> int bar() const
> {
>      // reference to ga, gb and gc is legal
>      // cannot modify ga (or gb or gc, obviously)
> }
> ...

Does not work as there is no context pointer to qualify and it would add 
strange limitations to const/... member functions and/or language 
inconsistencies.

> void main()
> {
>      int a = 1;
>      const int b = 2;
>      immutable int c = 3;
>
>      int innerFoo() immutable
>      {
>          // reference to c or gc is legal
>          // any reference to a, ga, b or ba is an error
>      }
>      int innerBar() const
>      {
>          // reference to a, ga, b, gb c and gc is legal
>          // cannot modify a or ga
>      }
> }
>
> For the inner functions, it's as if the const/immutable is applied to
> the context pointer, the same as it is applied to "this" in a
> class/struct method.
>
> This would be useful because it:
> a) provides better control over the exact behaviour of functions w.r.t.
> global/outer state.
> ...

I've brought this up a few times too. This _must_ be done. The current 
behaviour of DMD is unacceptable here.



More information about the Digitalmars-d mailing list