www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "shared" woes: shared instances of anonymous classes

reply Arafel <er.krali gmail.com> writes:
Hi!

I'm trying to wrap my mind around "shared", and I think I have managed 
to more or less grasp it. However I'm having a problem, and it seems 
it's just a missing feature (or rather combination of features) in the 
language (or I haven't found the right keyword combination).

Is there any way to create a shared instance of an anonymous class? 
Let's say:

```
class C {
	shared this() { }
}

void main() {
	shared C c = new /* shared */ C {
		shared this() {
			super();
		}
	});
}
```

This doesn't compile because, of course, the instantiation of the 
anonymous class is not shared.

However, the following code doesn't compile either, and I'm not even 
sure what "shared" is supposed to mean in this context:

```
class C {
	shared this() { }
}

void main() {
	shared C c = new shared(C) {
		shared this() {
			super();
		}
	});
}
```

I tried playing a bit around [1] (the non-shared constructors are 
needed), and ended up even more confused than before!!

Of course if I create a proper named class D, I can instantiate shared 
instances of D, so it's not like there's no workaround... still, coming 
from Java I like anonymous classes, and I think it'd be cool to be able 
to use them in this context.

If somebody knows how this works / is supposed to work, I'd be thankful!

[1]: https://dpaste.dzfl.pl/ce2ba93111a0
Jul 07 2017
next sibling parent Arafel <er.krali gmail.com> writes:
Well, in both snippets there and extra closing parenthesis... they are 
obviously a typo, blame copy and pasting and not cleaning up afterwards :)

On 07/07/2017 11:14 AM, Arafel wrote:
 Hi!
 
 I'm trying to wrap my mind around "shared", and I think I have managed 
 to more or less grasp it. However I'm having a problem, and it seems 
 it's just a missing feature (or rather combination of features) in the 
 language (or I haven't found the right keyword combination).
 
 Is there any way to create a shared instance of an anonymous class? 
 Let's say:
 
 ```
 class C {
      shared this() { }
 }
 
 void main() {
      shared C c = new /* shared */ C {
          shared this() {
              super();
          }
      });
 }
 ```
 
 This doesn't compile because, of course, the instantiation of the 
 anonymous class is not shared.
 
 However, the following code doesn't compile either, and I'm not even 
 sure what "shared" is supposed to mean in this context:
 
 ```
 class C {
      shared this() { }
 }
 
 void main() {
      shared C c = new shared(C) {
          shared this() {
              super();
          }
      });
 }
 ```
 
 I tried playing a bit around [1] (the non-shared constructors are 
 needed), and ended up even more confused than before!!
 
 Of course if I create a proper named class D, I can instantiate shared 
 instances of D, so it's not like there's no workaround... still, coming 
 from Java I like anonymous classes, and I think it'd be cool to be able 
 to use them in this context.
 
 If somebody knows how this works / is supposed to work, I'd be thankful!
 
 [1]: https://dpaste.dzfl.pl/ce2ba93111a0
Jul 07 2017
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 7 July 2017 at 09:14:56 UTC, Arafel wrote:
 [...]
 Is there any way to create a shared instance of an anonymous 
 class?
 [...]

 If somebody knows how this works / is supposed to work, I'd be 
 thankful!

 [1]: https://dpaste.dzfl.pl/ce2ba93111a0
Yes, but it's round about: you have to instantiate the class as unshared and then cast it to `shared` [1]. If you look at the grammar [2][3] you'll see why: NewAnonClassExpression does not support specifying the storage class for the new instance, as opposed to NewExpression. Do note, though, that `shared` is pretty much all rough edges with (virtually) no joy at present and IIRC from DConf2017 it's in the queue for an overhaul. [1] https://dpaste.dzfl.pl/35a9a8a1d1f7 [2] https://dlang.org/spec/grammar.html#NewExpression [3] https://dlang.org/spec/grammar.html#NewAnonClassExpression
Jul 07 2017