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

Is it possible to have support to closure replacement? #106

Open
samcortez opened this issue May 9, 2022 · 0 comments
Open

Is it possible to have support to closure replacement? #106

samcortez opened this issue May 9, 2022 · 0 comments

Comments

@samcortez
Copy link

I couldn't find such information anywhere and I'm a bit stuck on this one issue. I've been trying to write a new closure over an existing closure but I haven't been successful.

This is how you can reproduce it.

func closureReplacementTest() {
    
    do {
        
        // Initialize sample data
        var freshFoo = Foo()
        let freshBar = Bar()
        
        // Work on getting the property
        let objectToTest = try typeInfo(of: Foo.self)
        let property = try objectToTest.property(named: "closure")
        
        // Replace the closure to give 100 scores instead
        try property.set(value: { (bar: Bar) in
            bar.score = 100 // Actually crashing
        }, on: &freshFoo)
        
        // Execute what's supposed to be the new closure
        freshFoo.closure(freshBar)
        
        print("Score is: \(freshBar.score)") // Expects it to be 100

    } catch {}
}

I spend a few days reading posts and documentation regarding memory management but unfortunately that's not my forte. =)

I did debug as best as I could and I noticed that during the crash the memory address of the bar variable is different that it would be when I simply run the block directly. The only thing I think it's not working quite well is the address of the variables of the closure because that's when the crash happens as the code itself of the closure seems to run just fine.

Strongly appreciate any input on this, I'll still going to spend more days trying to figure out. Thanks!

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

1 participant