Content-Length: 274616 | pFad | http://github.com/microsoft/pxt-microbit/issues/6155

E4 Object Factory Error - variable used before it's declaration · Issue #6155 · microsoft/pxt-microbit · GitHub
Skip to content

Object Factory Error - variable used before it's declaration #6155

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

Closed
ssande-fwd opened this issue Apr 25, 2025 · 7 comments
Closed

Object Factory Error - variable used before it's declaration #6155

ssande-fwd opened this issue Apr 25, 2025 · 7 comments

Comments

@ssande-fwd
Copy link

ssande-fwd commented Apr 25, 2025

Describe the bug
I use an object factory to generate a Jacdac button client in on start. I have a custom on button down block for the button client I've created. The code is compiled such that the on button down code comes before the button client is generated. This leads to the error "variable used before it's declaration"

To Reproduce

  1. Use this extension https://github.com/ssande-fwd/demo-extension
  2. Open CustomButton drawer
  3. Create a button client in on start using the factory block
  4. Drag in an on button event block
  5. Observe the error associated with the on button event block

Expected behavior
I am able to use blocks associated with an object that is created in on start.

Screenshots
Image

Additional context
Why isn't on start code put at the start of the JavaScript compilation? Manually putting it there resolves the error.
The factory block is officially documented here https://makecode.com/defining-blocks

@abchatra
Copy link
Collaborator

Thanks for the feedback. On start can trigger events and can have pauses (which can also trigger some type of events). This is the reason events are registered first. I understand this breaks the above scenario. I am unsure of easy solution here.

@ssande-fwd
Copy link
Author

Sounds like you could use an init block in addition to on start.

@ssande-fwd
Copy link
Author

I'm pretty sure I could workaround it by making it a conditional with some behind-the-scenes logic. I was able to replicate the up, down, and hold events in MakeCode, so should be able to do it in the extension too.

Image

User-generated Jacdac clients is an experiment rather than a need, so I won't be pushing that idea to completion. I'm curious if you think an init block would be worthwhile for a future release though. Seems like this might be a niche situation if this is the first time it has come up.

@abchatra
Copy link
Collaborator

Like init event handler which is guaranteed to be registered first before all other event handlers?

@ssande-fwd
Copy link
Author

Yes, a block for initializing variables at the top of the code, basically. To guarantee they'll be available for events.

@abchatra abchatra transferred this issue from microsoft/pxt Apr 25, 2025
@riknoll
Copy link
Member

riknoll commented Apr 25, 2025

@ssande-fwd we have the afterOnStart comment annotation which you can use on your event to force the event handler to be placed after the compiled on-start code:

namespace something {
    //% block
    //% afterOnStart=1
    export function myEvent(handler: () => void) {
    }
}

you can also make your event nestable if you prefer so that you can place it after the variable declaration within on-start by using the handlerStatement annotation in a similar manner

@ssande-fwd
Copy link
Author

ssande-fwd commented Apr 28, 2025

Thank you! Both solutions solve the issue. I think afterOnStart would be the way to go. I couldn't find handlerStatement in the docs so here's what handlerStatement=1 block annotation does visually, for easy reference.

Image

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

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/microsoft/pxt-microbit/issues/6155

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy