www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Syntax: how to return shared?

reply Marek Janukowicz <marek janukowicz.net> writes:
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 () {
  ...
}

-- 
Marek Janukowicz
Aug 07 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 7 August 2015 at 17:19:16 UTC, Marek Janukowicz wrote:
 shared (shared Foo) foo () {
   ...
 }
That's correct, though the recommendation now is to put the other shared on teh right and change the parens a little: shared(Foo) foo() shared { } The ones without parens refer to the `this` in there and are kinda confusing to see on the left, so putting them on the right looks a bit easier (same with const btw).
Aug 07 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Aug 07 2015