Missing const feature? Having a thread own the read/write privilege
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Mon Jun 25 04:20:39 PDT 2007
Jason House wrote:
> For single-threaded applications, I believe an implicit cast to _scope_
> invariant is ok and *should* be allowed implicitly.
I believe you are wrong about that.
> If I call a
> function, it's guaranteed that the variable won't get modified by
> anything else while the function is executing.
No it isn't:
---
import std.stdio;
class Foo { int x; }
void bar(scope /* invariant */ Foo foo, void delegate() dg) {
writefln(foo.x);
dg();
writefln(foo.x);
}
void main() {
Foo f = new Foo;
f.x = 3;
bar(f, { f.x = 4; } );
}
---
Your suggestion would allow creation of a modifiable invariant reference
without requiring a cast.
Note that a similar example can also be constructing without passing a
delegate; a normal function call in bar() and a global mutable reference
would be just as effective.
It's only safe to allow implicitly casts to invariant when the compiler
can determine that no mutable references exist.
It may also be safe to implicitly cast to scope invariant if the
compiler can determine that no mutable references will be accessible (or
at least that they won't be used to write to it) during the lifetime of
the scope invariant reference.
The latter (no mutating accesses during lifetime) is probably pretty
hard to determine automatically in the general case unless you know the
former (no mutable refs exist), especially as you noted on
multithreading operating systems. (And I don't think the programming
language should change just because you start using threads)
More information about the Digitalmars-d
mailing list