Unofficial wish list status.(Jul 2008)
Jason House
jason.james.house at gmail.com
Wed Jul 23 12:09:37 PDT 2008
Steven Schveighoffer Wrote:
> "Jason House" wrote
> > Walter Bright Wrote:
> >> The missing ingredient here is what the 'pure' modifier for functions
> >> provides. That is the ability to diagnose attempts to alter global state
> >> from within a function that is not supposed to do that. Invariant does
> >> not imply pure.
> >
> >
> > That's actually a very key part to my whole point. There is very little
> > difference between pure and invariant functions. Why do we need a concept
> > for both? Under the assumption that we're adding const to support
> > functional programming, I don't understand the motivation for this
> > difference.
>
> You are misunderstanding what an 'invariant' member function is. The
> function itself is not invariant. It's a function whose 'this' pointer is
> marked as invariant.
It's not that I misunderstand but that I question. Given the goals of the const system, invariant functions, as defined, don't make a lot of sense. They make some sense, but not a lot of sense :)
> Without invariant member functions, you could not have a function like
> printTo for an invariant class instance. A pure function would not be able
> to print, because the stream argument would also have to be invariant, and
> most likely, stream.print would not be a pure function.
It's true that what I'm talking about implies that such a thing could not occur. In C++ const (and the current D const), that type of construct is possible. It really comes down to if const exists for multithreading or if it exists for faking pass by value.
I know Walter has tried to eliminate loopholes with the pass by value semantics with the transitive const. For example, transitive const protects tree manipulations such as parent.child.property = 1; from violating const, but it doesn't stop global_root.child.property = 1;.
Such loopholes are not good when compilers are trying to optimize. I also can't really get past how the const system allows people to bypass "mutable members" through the use of global variables.
More information about the Digitalmars-d
mailing list