Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makeObserver in a loop / why does it use static struct? #38

Open
Robert-M-Muench opened this issue Nov 24, 2019 · 2 comments
Open

makeObserver in a loop / why does it use static struct? #38

Robert-M-Muench opened this issue Nov 24, 2019 · 2 comments

Comments

@Robert-M-Muench
Copy link

Robert-M-Muench commented Nov 24, 2019

makeObserver uses a static struct AnonymouseObserver{...}which, from my understanding, means that there is only one instance of this struct per thread. Hence, when I do this:

foreach(thing; myThings){
 myMasterThing.subscribe(
  makeObserver(
   (float f) {thing.myFunc(f);},
   (Exception) {}
  )
 );
}

I get a lof of observers, but they all point to the same function of the last thing in the loop.

But I would like to have an observer per thing. How to do this?

@Robert-M-Muench
Copy link
Author

The solution seems to be to change it to:

foreach(thing; myThings){
 myMasterThing.subscribe(&thing.myFunc);
}

and have

myMasterThing.subscribe(void delegate() doPut){
 doSubscribe(myMasterThingStream, makeObserver(doPut, (Exception) {}));
}

@r4j4h
Copy link

r4j4h commented Jun 7, 2020

Hi there, a little late but I think your initial approach might still be feasible.

I ran into this myself recently thankfully and here is a workaround I found. It ends up this is sort of a compiler bug! https://issues.dlang.org/show_bug.cgi?id=2043#c8 and https://issues.dlang.org/show_bug.cgi?id=2043#c10 clarify a way to "trick" the system into giving you a delegate per-iteration, and you can follow that thread for more info - it's all in the same bug thread.

(edit) sorry I might have jumped the gun on responding when I saw the foreach and that you were getiing the last thing in the loop because I didn't pay attention to the use of static. This may not work there. 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants