digitalmars.D - Function without 'this' cannot be 'shared'
- Ruby The Roobster (22/22) Dec 25 2021 Take the following code:
- Adam D Ruppe (10/11) Dec 25 2021 What do you expect that second shared to do?
- Ruby The Roobster (2/13) Dec 25 2021 Thank you. It now works.
Take the following code:
```d
void main() //Can be empty, problem occurs regardless
{
}
int test(int a) //Test function.
{
return 3 * a;
}
//Now overload it for shared...
int test(shared int a) shared
{
return cast(shared(int))test(cast(int)a); //Note: The
problem still happens regardless whether you have this line or
not.
}
```
Compiling on the latest version, the compiler output you get
should be(assuming the file is named test.d):
```
Error: function 'test.test' without 'this' cannot be 'shared'
```
Dec 25 2021
On Saturday, 25 December 2021 at 19:46:20 UTC, Ruby The Roobster wrote:int test(shared int a) sharedWhat do you expect that second shared to do? Generally qualifiers after the parenthesis apply to the hidden `this` parameter, and since there isn't one that's why it is failing. If you wanted it to apply to the return value, you write that shared(int) test() {} and if you wanted something else, well it depends on what you wanted.
Dec 25 2021
On Saturday, 25 December 2021 at 20:03:49 UTC, Adam D Ruppe wrote:On Saturday, 25 December 2021 at 19:46:20 UTC, Ruby The Roobster wrote:Thank you. It now works.int test(shared int a) sharedWhat do you expect that second shared to do? Generally qualifiers after the parenthesis apply to the hidden `this` parameter, and since there isn't one that's why it is failing. If you wanted it to apply to the return value, you write that shared(int) test() {} and if you wanted something else, well it depends on what you wanted.
Dec 25 2021








Ruby The Roobster <michaeleverestc79 gmail.com>