Syntax: how to return shared?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 7 11:15:09 PDT 2015


On 8/7/15 1:19 PM, Marek Janukowicz wrote:
> How do I mark a function as returning shared object?
>
> This won't compile:
>
> shared Foo foo () {
>    ...
> }
>
> This does, but looks somewhat awkward to me:
>
> shared (shared Foo) foo () {
>    ...
> }
>

shared, const, immutable when applied to a member function actually are 
NOT affecting the return type.

So for instance:

const Foo foo()

Does NOT return a const Foo, but:

const(Foo) foo()

does. The first returns a mutable Foo, and applies const to the 'this' 
parameter for the foo member function (a hidden parameter).

So what you likely want is:

shared(Foo) foo()

-Steve


More information about the Digitalmars-d-learn mailing list