Why do "const inout" and "const inout shared" exist?

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 3 11:49:56 PDT 2017


On 7/3/2017 8:48 AM, Petar Kirov [ZombineDev] wrote:
> Unlike C++, in D objects can't be shared across threads, unless they are marked 
> as `shared` (modulo un-`@safe` code - like casts and `__gshared` - and compiler 
> bugs).
> I.e. non-`shared` objects can't be mutated by more than one thread.
> Combined with `pure` functions, the guarantees provided by D's type system are 
> quite useful:
> 
> ```
> void main()
> {
>      int x = globalArray.foo();
> }
> 
> // module-level non-shared variables are thread-local
> int[] globalArray = [1, 2, 3, 4];
> 
> int foo(const(int)[] array) pure
> {
>      // globalArray[0] = 42; doesn't compile
>      // For all intents and purposes, the elements of `array` can
>      // be viewed as immutable here and they are *not* aliased.
> 
>      // ...
> }

Keep in mind that today's optimizers are pretty much tuned to what works for 
C++. While D's particular semantics offer opportunities for optimizations, they 
are currently pretty much unexploited.


More information about the Digitalmars-d mailing list