Thread overview | |||||
---|---|---|---|---|---|
|
August 07, 2015 Syntax: how to return shared? | ||||
---|---|---|---|---|
| ||||
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
|
August 07, 2015 Re: Syntax: how to return shared? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marek Janukowicz | 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).
|
August 07, 2015 Re: Syntax: how to return shared? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marek Janukowicz | 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
|
Copyright © 1999-2021 by the D Language Foundation