On 05/21/2016 02:17 PM, chmike wrote:
On Saturday, 21 May 2016 at 10:42:13 UTC, chmike wrote:
source/app.d(23,27): Error: cannot implicitly convert expression (one)
of type immutable(Obj) to app.Info
Apparently Rebindable doesn't support polymorphism. This is hopefully
fixable.
No, the difference in mutability is the problem. Can't implicitly
convert class objects from immutable to mutable.
[...]
A Rebindable variable can't be used as a switch argument. This would
require a change to the language rules.
Class objects in general can't be used as switch arguments.
However, the static immutable
object Infos.one can be used as a case argument.
How do you figure that? I get 'Error: case must be a string or an
integral constant, not Obj("I'm one")'.
The conclusion is that Rebindable doesn't cover the needs of a mutable
object reference.
I don't follow. Seems to me that the limitations you see here are in
place for ordinary class objects as well.
In the flyweight pattern we only need to compare object addresses and we
also want to use the lazy pattern to instantiate the immutable instances.
It may be possible to get pointers from the objects and switch over
those pointers. Can't use class objects directly, because equality is
defined by opEquals which is virtual. And a switch doesn't make sense if
opEquals needs to be called.
Also, I'm afraid you're going to have a bad time when trying to get lazy
initialization + immutable.